Skip to content

Commit

Permalink
Initialize lazy properties if hydrating into a Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
malarzm committed Jan 31, 2016
1 parent 7a6bdee commit 16abe58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
9 changes: 9 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@ public function hydrate($document, $data, array $hints = array())
$data = $this->getHydratorFor($metadata->name)->hydrate($document, $data, $hints);
if ($document instanceof Proxy) {
$document->__isInitialized__ = true;
$document->__setInitializer(null);
$document->__setCloner(null);
// lazy properties may be left uninitialized
$properties = $document->__getLazyProperties();
foreach ($properties as $propertyName => $property) {
if ( ! isset($document->$propertyName)) {
$document->$propertyName = $properties[$propertyName];
}
}
}

// Invoke the postLoad lifecycle callbacks and listeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ class GH1346Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
*/
public function testPublicProperty()
{

$refrenced1 = new GH1346ReferencedDocument();
$refrenced2 = new GH1346ReferencedDocument();
$referenced1 = new GH1346ReferencedDocument();
$referenced2 = new GH1346ReferencedDocument();
$gH1346Document = new GH1346Document();
$gH1346Document->addReference($refrenced1);
$gH1346Document->addReference($referenced1);

$this->dm->persist($refrenced2);
$this->dm->persist($refrenced1);
$this->dm->persist($referenced2);
$this->dm->persist($referenced1);
$this->dm->persist($gH1346Document);
$this->dm->flush();
$this->dm->clear();

$gH1346Document = $this->dm->getRepository(__NAMESPACE__ . '\GH1346Document')->find($gH1346Document->getId());
$refrenced2 = $this->dm->getRepository(__NAMESPACE__ . '\GH1346ReferencedDocument')->find($refrenced2->getId());
$referenced2 = $this->dm->getRepository(__NAMESPACE__ . '\GH1346ReferencedDocument')->find($referenced2->getId());

$gH1346Document->addReference($refrenced2);
$gH1346Document->addReference($referenced2);

$this->dm->persist($gH1346Document);
$this->dm->flush();
Expand Down Expand Up @@ -76,7 +75,7 @@ public function getReferences()
*/
class GH1346ReferencedDocument
{
/** @ODM\String() */
/** @ODM\Field(type="string") */
public $test;

/** @ODM\Id */
Expand Down

0 comments on commit 16abe58

Please sign in to comment.