[英]Doctrine ODM association not loaded
I've started working with Doctrine MongoDB ODM. 我已经开始使用Doctrine MongoDB ODM。 I have a Document called Document
which has an association originalFile
to another document called File
. 我有一个称为Document
,它具有与originalFile
到另一个称为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
: 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. 文档和文件文档存储在MongoDB中。 When I'm loading the Document
the $originalFile
property is null
. 当我加载Document
, $originalFile
属性为null
。 I have no clue what's missing. 我不知道缺少什么。 Is the mapping wrong or is this simply a bug in Doctrine? 映射是否错误,或者这仅仅是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();
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
. 我唯一要做的就是composer update
。 Maybe a bug correlating with my setup. 可能是与我的设置相关的错误。
I got such problem, it is related to metadata cache, just run 我遇到了这样的问题,它与元数据缓存有关,只是运行
bin/console cache:clear
bin/console doctrine:cache:clear-metadata
Also good to know these commands, if you are using symfony built-in cache 如果您使用的是symfony内置缓存,也很了解这些命令
bin/console doctrine:cache:clear-query
bin/console doctrine:cache:clear-result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.