diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index e4ce99879..cf5e4c52c 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1124,6 +1124,10 @@ class UnitOfWork implements PropertyChangedListener if (isset($this->entityIdentifiers[$oid])) { $this->addToIdentityMap($entity); } + + if ($entity instanceof NotifyPropertyChanged) { + $entity->addPropertyChangedListener($this); + } } /** @@ -1302,10 +1306,6 @@ class UnitOfWork implements PropertyChangedListener $this->identityMap[$className][$idHash] = $entity; - if ($entity instanceof NotifyPropertyChanged) { - $entity->addPropertyChangedListener($this); - } - return true; } @@ -2364,11 +2364,12 @@ class UnitOfWork implements PropertyChangedListener } } else { $entity = $this->newInstance($class); - $oid = spl_object_hash($entity); + $oid = spl_object_hash($entity); - $this->entityIdentifiers[$oid] = $id; - $this->entityStates[$oid] = self::STATE_MANAGED; + $this->entityIdentifiers[$oid] = $id; + $this->entityStates[$oid] = self::STATE_MANAGED; $this->originalEntityData[$oid] = $data; + $this->identityMap[$class->rootEntityName][$idHash] = $entity; if ($entity instanceof NotifyPropertyChanged) { @@ -2800,6 +2801,10 @@ class UnitOfWork implements PropertyChangedListener $this->originalEntityData[$oid] = $data; $this->addToIdentityMap($entity); + + if ($entity instanceof NotifyPropertyChanged) { + $entity->addPropertyChangedListener($this); + } } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php b/tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php index 8d9d3ffa6..069e11c23 100644 --- a/tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php @@ -40,9 +40,16 @@ class NotifyPolicyTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->persist($user); $this->_em->persist($group); + + $this->assertEquals(1, count($user->listeners)); + $this->assertEquals(1, count($group->listeners)); + $this->_em->flush(); $this->_em->clear(); + $this->assertEquals(1, count($user->listeners)); + $this->assertEquals(1, count($group->listeners)); + $userId = $user->getId(); $groupId = $group->getId(); unset($user, $group); @@ -52,6 +59,9 @@ class NotifyPolicyTest extends \Doctrine\Tests\OrmFunctionalTestCase $group = $this->_em->find(__NAMESPACE__.'\NotifyGroup', $groupId); $this->assertEquals(1, $group->getUsers()->count()); + $this->assertEquals(1, count($user->listeners)); + $this->assertEquals(1, count($group->listeners)); + $group2 = new NotifyGroup(); $group2->setName('nerds'); $this->_em->persist($group2); @@ -63,6 +73,9 @@ class NotifyPolicyTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->flush(); $this->_em->clear(); + $this->assertEquals(1, count($user->listeners)); + $this->assertEquals(1, count($group->listeners)); + $group2Id = $group2->getId(); unset($group2, $user); @@ -77,7 +90,7 @@ class NotifyPolicyTest extends \Doctrine\Tests\OrmFunctionalTestCase } class NotifyBaseEntity implements NotifyPropertyChanged { - private $listeners = array(); + public $listeners = array(); public function addPropertyChangedListener(PropertyChangedListener $listener) { $this->listeners[] = $listener;