1

Using Symfony2 and Doctrine ODM with MongoDB, I need to access the parent document from within an embedded document.

I have a 'Cardset' object which has embedMany on 'Card' objects.

The 'Card' object has a 'save image' method and I want that to use the ID of the Cardset object that contains it as part of the path to which it saves.

I can't find a way to access the parent document from within the embeddeddocument.

If I could get hold of an instance of 'DocumentManager', I might be able to use the 'getParentAssociation()' method. But this

$this->get('doctrine.odm.mongodb.document_manager');

doesn't work since it's not inside a Controller. I'm not experienced enough to know how to make the DocumentManager into a service that would be available from inside my object class.

Can anyone help?

Adam Knowles
  • 492
  • 5
  • 14

1 Answers1

2

You theorically can access this using the UnitOfWork:

$uo = $this->get('doctrine.odm.mongodb.document_manager')->getUnitOfWork();
list($mapping, $parent, $propertyPath) = $uo->getParentAssociation($embeddedDocument); 

EDIT: You should'nt get it from your entities/documents (or embedded documents). But if you want to, you will have to inject it using a listener on "postLoad": http://readthedocs.org/docs/doctrine-mongodb-odm/en/latest/reference/events.html?highlight=postload#lifecycle-events

Florian Klein
  • 8,692
  • 1
  • 32
  • 42
  • The root cause of my issue was that I shouldn't be doing this, that this whole chunk of code belongs elsewhere. See my subsequent question http://stackoverflow.com/questions/9440519/symfony2-mvc-where-does-my-code-belong The fact I couldn't access the '$container' or document manager from within my entity was telling me it's not the right thing to do. Since your answer mentions this Florian (though I'm fairly certain your post before the 'edit' is wrong), I'm marking your answer as accepted with thanks. – Adam Knowles Feb 25 '12 at 12:32