From 845c85552ea29603e2fcc10f6f89d8f7bb3dea9a Mon Sep 17 00:00:00 2001 From: beberlei Date: Mon, 7 Dec 2009 22:10:40 +0000 Subject: [PATCH] [2.0] DDC-194 - Fixed infinite recursion issue with references and @PostLoad annotation by telling proxy to be loaded before calling EntityPersister->load... --- lib/Doctrine/ORM/Proxy/ProxyFactory.php | 2 +- .../ORM/Functional/LifecycleCallbackTest.php | 24 +++++++++++++++++++ .../ORM/Functional/ReferenceProxyTest.php | 3 +-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Proxy/ProxyFactory.php b/lib/Doctrine/ORM/Proxy/ProxyFactory.php index 13cc4aae6..9a2812f27 100644 --- a/lib/Doctrine/ORM/Proxy/ProxyFactory.php +++ b/lib/Doctrine/ORM/Proxy/ProxyFactory.php @@ -261,10 +261,10 @@ namespace { } private function _load() { if ( ! $this->_loaded) { + $this->_loaded = true; $this->_entityPersister->load($this->_identifier, $this); unset($this->_entityPersister); unset($this->_identifier); - $this->_loaded = true; } } public function __isInitialized__() { return $this->_loaded; } diff --git a/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php b/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php index b655d8cea..2208dfd34 100644 --- a/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php @@ -59,6 +59,26 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals('Alice', $user2->getName()); $this->assertEquals('Hello World', $user2->getValue()); } + + /** + * @group DDC-194 + */ + public function testGetReferenceWithPostLoadEventIsDelayedUntilProxyTrigger() + { + $entity = new LifecycleCallbackTestEntity; + $entity->value = 'hello'; + $this->_em->persist($entity); + $this->_em->flush(); + $id = $entity->getId(); + + $this->_em->clear(); + + $reference = $this->_em->getReference('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id); + $this->assertFalse($reference->postLoadCallbackInvoked); + + $reference->getId(); // trigger proxy load + $this->assertTrue($reference->postLoadCallbackInvoked); + } } /** @Entity @HasLifecycleCallbacks */ @@ -99,6 +119,10 @@ class LifecycleCallbackTestEntity * @Column(type="string") */ public $value; + + public function getId() { + return $this->id; + } /** @PrePersist */ public function doStuffOnPrePersist() { diff --git a/tests/Doctrine/Tests/ORM/Functional/ReferenceProxyTest.php b/tests/Doctrine/Tests/ORM/Functional/ReferenceProxyTest.php index 762b43c84..38f9b8429 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ReferenceProxyTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ReferenceProxyTest.php @@ -11,11 +11,10 @@ require_once __DIR__ . '/../../TestInit.php'; /** * Tests the generation of a proxy object for lazy loading. * @author Giorgio Sironi + * @author Benjamin Eberlei */ class ReferenceProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase { - private $product; - protected function setUp() { $this->useModelSet('ecommerce');