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();