dongwei2610 2014-06-23 08:39
浏览 39
已采纳

Doctrine ODM关联未加载

I've started working with Doctrine MongoDB ODM. I have a Document called Document which has an association originalFile to another document called File.

class Document extends AbstractDocument
{
/**
 * @var integer $id
 *
 * @MongoDB\Id
 */
protected $id;

/**
 * @var ImportSource
 *
 * @MongoDB\ReferenceOne(targetDocument="ImportSource", cascade="all", simple=true)
 */
protected $importSource;

/**
 * @var DocumentState
 *
 * @MongoDB\ReferenceOne(targetDocument="DocumentState", cascade="all", simple=true)
 */
protected $state;

/**
 * @var \DateTime $created
 *
 * @MongoDB\Date
 */
protected $created;

/**
 * @var \DateTime $modified
 *
 * @MongoDB\Date
 */
protected $modified;

/**
 * @var File $formattedFile
 *
 * @MongoDB\ReferenceOne(targetDocument="File", cascade="all")
 */
protected $formattedFile;

/**
 * @var File $originalFilename
 *
 * @MongoDB\ReferenceOne(targetDocument="File", cascade="all")
 */
protected $originalFile;

//getters, setters etc..
}

File:

class File
{
/**
 * @var string
 *
 * @MongoDB\Id
 */
protected $id;

/**
 * @var FileType
 *
 * @MongoDB\ReferenceOne(targetDocument="FileType", cascade="all", simple=true)
 */
protected $type;

/**
 * @var string
 *
 * @MongoDB\String
 */
protected $basename;

/**
 * @var \DateTime
 *
 * @MongoDB\Date
 */
protected $created;

/**
 * @var \DateTime
 *
 * @MongoDB\Date
 */
protected $deleted;

/**
 * @var \MongoGridFSFile
 *
 * @MongoDB\File
 */
protected $file;

Storing the Document works without any problems. The Document and the File-Document are stored in MongoDB. When I'm loading the Document the $originalFile property is null. I have no clue what's missing. Is the mapping wrong or is this simply a bug in Doctrine?

Edit:

This is how I'm storing the documents:

//create if not exists
if ($documentObj === null) {
    $documentObj = new Document();
    $documentObj->setCreated(new \DateTime());
    //create file
    $file = new File();

} else {
    $documentObj->setModified(new \DateTime());
    //get file
    $file = $documentObj->getOriginalFile();
}
$file->setFile($pathToFile);
//set file
$documentObj->setOriginalFile($file);
//set import source
$documentObj->setImportSource($source);
//store document
$documents[] = $this->storeDocument($documentObj, $documentFields);

//... method storeDocument fills other properties of the document and finally persits the document:
$this->manager->persist($documentObj);
$this->manager->flush();
  • 写回答

2条回答 默认 最新

  • dongyuntao2000 2014-07-03 07:36
    关注

    I have no clue why, but it's working now... I was occupied with other projects for a few days, returned to this projects, ran my unit tests again and everything worked. Only thing I've done was a composer update. Maybe a bug correlating with my setup.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址