Skip to content

Commit 01b334a

Browse files
committed
Merge pull request #1355 from malarzm/proxy-public-prop
Fix initializing lazy properties during hydration
2 parents 21c8a94 + 16abe58 commit 01b334a

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,15 @@ public function hydrate($document, $data, array $hints = array())
453453
$data = $this->getHydratorFor($metadata->name)->hydrate($document, $data, $hints);
454454
if ($document instanceof Proxy) {
455455
$document->__isInitialized__ = true;
456+
$document->__setInitializer(null);
457+
$document->__setCloner(null);
458+
// lazy properties may be left uninitialized
459+
$properties = $document->__getLazyProperties();
460+
foreach ($properties as $propertyName => $property) {
461+
if ( ! isset($document->$propertyName)) {
462+
$document->$propertyName = $properties[$propertyName];
463+
}
464+
}
456465
}
457466

458467
// Invoke the postLoad lifecycle callbacks and listeners
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
7+
8+
class GH1346Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
9+
{
10+
/**
11+
* @group GH1346Test
12+
*/
13+
public function testPublicProperty()
14+
{
15+
$referenced1 = new GH1346ReferencedDocument();
16+
$referenced2 = new GH1346ReferencedDocument();
17+
$gH1346Document = new GH1346Document();
18+
$gH1346Document->addReference($referenced1);
19+
20+
$this->dm->persist($referenced2);
21+
$this->dm->persist($referenced1);
22+
$this->dm->persist($gH1346Document);
23+
$this->dm->flush();
24+
$this->dm->clear();
25+
26+
$gH1346Document = $this->dm->getRepository(__NAMESPACE__ . '\GH1346Document')->find($gH1346Document->getId());
27+
$referenced2 = $this->dm->getRepository(__NAMESPACE__ . '\GH1346ReferencedDocument')->find($referenced2->getId());
28+
29+
$gH1346Document->addReference($referenced2);
30+
31+
$this->dm->persist($gH1346Document);
32+
$this->dm->flush();
33+
34+
$this->assertEquals(2, $gH1346Document->getReferences()->count());
35+
36+
$this->dm->flush();
37+
}
38+
}
39+
40+
41+
/**
42+
* @ODM\Document
43+
*/
44+
class GH1346Document
45+
{
46+
/** @ODM\Id */
47+
protected $id;
48+
49+
/** @ODM\ReferenceMany(targetDocument="GH1346ReferencedDocument") */
50+
protected $references;
51+
52+
public function __construct()
53+
{
54+
$this->references = new ArrayCollection();
55+
}
56+
57+
public function getId()
58+
{
59+
return $this->id;
60+
}
61+
62+
public function addReference($otherReference)
63+
{
64+
$this->references->add($otherReference);
65+
}
66+
67+
public function getReferences()
68+
{
69+
return $this->references;
70+
}
71+
}
72+
73+
/**
74+
* @ODM\Document
75+
*/
76+
class GH1346ReferencedDocument
77+
{
78+
/** @ODM\Field(type="string") */
79+
public $test;
80+
81+
/** @ODM\Id */
82+
protected $id;
83+
84+
public function setTest($test)
85+
{
86+
$this->test = $test;
87+
}
88+
89+
public function getId()
90+
{
91+
return $this->id;
92+
}
93+
}

0 commit comments

Comments
 (0)