0
votes

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
Possibly might help if you showed some code of how you are "storing" and how you are "loading". You are not giving anyone anything to "go on" other than commented out code. - Neil Lunn
I've added the code responsible for storing the document. - Frido
Thanks for posting some code, but there has to be more than this really. Thinking logically. Why would doctrine suddenly just have a problem with this one field? There has to be a reason, that will be defined in something you are not sharing. Worth looking at and/or sharing if you cannot work that out yourself. - Neil Lunn

2 Answers

0
votes

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.

0
votes

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

bin/console doctrine:cache:clear-query  
bin/console doctrine:cache:clear-result