How to force Doctrine MongoDB ODM Document Proxy to convert to the "original" document?

I have a Person document specified in a User document. When I retrieve a user, it does not have a built-in Person object, but a Person proxy object. Is there a way to “force” a proxy server to become a “complete” document (therefore Person proxy => Person).

I tried to call a method to get additional data (so __load starts, but the object remains the version of the "proxy".

I hope someone can shed more light on this than the ODM documentation.

+7
source share
2 answers

You can accomplish this using the Link to links .

Examples of documents:

/** @Document */ class User { /** @ReferenceOne(targetDocument="Person") */ private $person; } /** @Document */ class Person { // ... } 

Using QueryBuilder:

 /* @var $user User */ $user = $dm->createQueryBuilder('User') ->field('person')->prime(true) ->getQuery() ->getSingleResult(); 
+2
source

You do not need to retrieve the source object, since the Proxy class must be 100% transparent to your code.

If you need to serialize a document, for example, to send it via the API, be sure to correctly implement the serialize() method in your document.

If you still need to get a referenced document without a proxy, you can either prime() it, or get it using a separate request specifying hydrate(false) :

 $user = $dm->createQueryBuilder('Person') ->field('_id')->equals($user->getPerson()->getId()) ->hydrate(false) 

See: Doctrine ODM Doc: Disable Hydration for more information.

+2
source

All Articles