I have a document that has a document embedded. When I create an object for the first time everything works fine, but when I try to update a document, the embedded document does not get updated.
/** @MongoDB\Document */
class DocumentA
{
/** @MongoDB\EmbedOne(targetDocument="DocumentB") **/
protected $docB;
/** @MongoDB\String */
protected $valueA;
}
/** @MongoDB\EmbeddedDocument */
class DocumentB
{
/** @MongoDB\String */
protected $valueB;
}
In my application I query for a document, update the values and persist them to the data store.
// Variant A – Does not work
$document = ... // find from data store
$document->setValueA('Hello World');
$document->getDocB()->setValueB('foo baz');
$om->persist($document);
$om->flush();
If I do not update the embedded document, but set a new one everything works fine:
// Variant B - Does work
$document = ... // find from data store
$document->setValueB('Hello World 2');
$document->setDocB(new DocumentB());
$document->getDocB()->setValueB('foo baz 2');
$om->persist($document);
$om->flush();
As I said, Variant B works fine. However, in my application the documents are more complex and it would be impractical for me to create a new object for the embedded document every time I have to update it. Is there a method to tell Doctrine ODM to look at the values of an embedded document to decide if it should be updated?