diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index e982d6b8b..745b0322e 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2498,14 +2498,14 @@ class UnitOfWork implements PropertyChangedListener && isset($hints[Query::HINT_REFRESH_ENTITY]) && ($unmanagedProxy = $hints[Query::HINT_REFRESH_ENTITY]) !== $entity && $unmanagedProxy instanceof Proxy - && (($unmanagedProxyClass = $this->em->getClassMetadata(get_class($unmanagedProxy))) === $class) + && $this->isIdentifierEquals($unmanagedProxy, $entity) ) { // DDC-1238 - we have a managed instance, but it isn't the provided one. // Therefore we clear its identifier. Also, we must re-fetch metadata since the // refreshed object may be anything - foreach ($unmanagedProxyClass->identifier as $fieldName) { - $unmanagedProxyClass->reflFields[$fieldName]->setValue($unmanagedProxy, null); + foreach ($class->identifier as $fieldName) { + $class->reflFields[$fieldName]->setValue($unmanagedProxy, null); } return $unmanagedProxy; @@ -3214,4 +3214,37 @@ class UnitOfWork implements PropertyChangedListener $this->evm->dispatchEvent(Events::postFlush, new PostFlushEventArgs($this->em)); } } + + /** + * Verifies if two given entities actually are the same based on identifier comparison + * + * @param object $entity1 + * @param object $entity2 + * + * @return bool + */ + private function isIdentifierEquals($entity1, $entity2) + { + if ($entity1 === $entity2) { + return true; + } + + $class = $this->em->getClassMetadata(get_class($entity1)); + + if ($class !== $this->em->getClassMetadata(get_class($entity2))) { + return false; + } + + $oid1 = spl_object_hash($entity1); + $oid2 = spl_object_hash($entity2); + + $id1 = isset($this->entityIdentifiers[$oid1]) + ? $this->entityIdentifiers[$oid1] + : $this->flattenIdentifier($class, $class->getIdentifierValues($entity1)); + $id2 = isset($this->entityIdentifiers[$oid2]) + ? $this->entityIdentifiers[$oid2] + : $this->flattenIdentifier($class, $class->getIdentifierValues($entity2)); + + return $id1 === $id2 || implode(' ', $id1) === implode(' ', $id2); + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2931Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2931Test.php new file mode 100755 index 000000000..90258b75b --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2931Test.php @@ -0,0 +1,119 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2931User'), + )); + } catch (\Exception $e) { + // no action needed - schema seems to be already in place + } + } + + public function testIssue() + { + $first = new DDC2931User(); + $second = new DDC2931User(); + $third = new DDC2931User(); + + $second->parent = $first; + $third->parent = $second; + + $this->_em->persist($first); + $this->_em->persist($second); + $this->_em->persist($third); + + $this->_em->flush(); + $this->_em->clear(); + + $second = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC2931User', $second->id); + + $this->assertSame(2, $second->getRank()); + } + + public function testFetchJoinedEntitiesCanBeRefreshed() + { + $first = new DDC2931User(); + $second = new DDC2931User(); + $third = new DDC2931User(); + + $second->parent = $first; + $third->parent = $second; + + $first->value = 1; + $second->value = 2; + $third->value = 3; + + $this->_em->persist($first); + $this->_em->persist($second); + $this->_em->persist($third); + + $this->_em->flush(); + + $first->value = 4; + $second->value = 5; + $third->value = 6; + + $refreshedSecond = $this + ->_em + ->createQuery( + 'SELECT e, p, c FROM ' + . __NAMESPACE__ . '\\DDC2931User e LEFT JOIN e.parent p LEFT JOIN e.child c WHERE e = :id' + ) + ->setParameter('id', $second) + ->setHint(Query::HINT_REFRESH, true) + ->getResult(); + + $this->assertCount(1, $refreshedSecond); + $this->assertSame(1, $first->value); + $this->assertSame(2, $second->value); + $this->assertSame(3, $third->value); + } +} + + +/** @Entity */ +class DDC2931User +{ + + /** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */ + public $id; + + /** @OneToOne(targetEntity="DDC2931User", inversedBy="child") */ + public $parent; + + /** @OneToOne(targetEntity="DDC2931User", mappedBy="parent") */ + public $child; + + /** @Column(type="integer") */ + public $value = 0; + + /** + * Return Rank recursively + * My rank is 1 + rank of my parent + * @return integer + */ + public function getRank() + { + return 1 + ($this->parent ? $this->parent->getRank() : 0); + } + + public function __wakeup() + { + echo 'foo'; + } +}