Skip to content

Commit

Permalink
Merge pull request #1355 from malarzm/proxy-public-prop
Browse files Browse the repository at this point in the history
Fix initializing lazy properties during hydration
  • Loading branch information
malarzm committed Feb 8, 2016
2 parents 21c8a94 + 16abe58 commit 01b334a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 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
93 changes: 93 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1346Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

class GH1346Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
/**
* @group GH1346Test
*/
public function testPublicProperty()
{
$referenced1 = new GH1346ReferencedDocument();
$referenced2 = new GH1346ReferencedDocument();
$gH1346Document = new GH1346Document();
$gH1346Document->addReference($referenced1);

$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());
$referenced2 = $this->dm->getRepository(__NAMESPACE__ . '\GH1346ReferencedDocument')->find($referenced2->getId());

$gH1346Document->addReference($referenced2);

$this->dm->persist($gH1346Document);
$this->dm->flush();

$this->assertEquals(2, $gH1346Document->getReferences()->count());

$this->dm->flush();
}
}


/**
* @ODM\Document
*/
class GH1346Document
{
/** @ODM\Id */
protected $id;

/** @ODM\ReferenceMany(targetDocument="GH1346ReferencedDocument") */
protected $references;

public function __construct()
{
$this->references = new ArrayCollection();
}

public function getId()
{
return $this->id;
}

public function addReference($otherReference)
{
$this->references->add($otherReference);
}

public function getReferences()
{
return $this->references;
}
}

/**
* @ODM\Document
*/
class GH1346ReferencedDocument
{
/** @ODM\Field(type="string") */
public $test;

/** @ODM\Id */
protected $id;

public function setTest($test)
{
$this->test = $test;
}

public function getId()
{
return $this->id;
}
}

0 comments on commit 01b334a

Please sign in to comment.