How to force a Doctrine MongoDB ODM Document Proxy to convert to the 'original' document?
Asked Answered
F

2

8

I have a document Person referenced in document User. When I retrieve User, it doesn't have a Person object embedded, but a Person proxy object. Is there a way to "force" the proxy to become a "full" document (so Person proxy => Person).

I've tried calling a method to retrieve additional data (so __load gets triggered, but the object remains the 'proxy' version.

I hope someone can shed more light on this than the ODM's documention does.

Flofloat answered 8/6, 2011 at 14:23 Comment(2)
that sounds like you're trying to do a relationship in a non-relational databaseCavalryman
well, I believe what you're pointing out is Doctrine lazy loading referenced information? How exactly is this bothering your application or the problem you are trying to solve?Cantara
O
3

You can accomplish this by Priming References.

Example Documents:

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

/** @Document */
class Person
{
    // ...
}

Using the QueryBuilder:

/* @var $user User */
$user = $dm->createQueryBuilder('User')
    ->field('person')->prime(true)
    ->getQuery()
    ->getSingleResult();
Ostiary answered 1/4, 2012 at 2:18 Comment(1)
This query throw a exception, because after use prime you can't use limit like on getSingleResult().Darcee
W
2

You shouldn't need to extract the original object, since the Proxy class should be 100% transparent to your code.

If you need to serialize the document, for example to send it through an API, be sure to correctly implement the serialize() method on your Document.

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

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

See: Doctrine ODM Doc: Disabling hydration for more info.

Wornout answered 29/4, 2013 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.