From a0fc09855bc4432b5475a051a45255dc5c85472d Mon Sep 17 00:00:00 2001 From: beberlei Date: Sun, 28 Feb 2010 14:14:44 +0000 Subject: [PATCH] [2.0] DDC-353 - Fix UnitOfWork::createEntity and StandardPersister::refresh() not marking created Proxy instances as MANAGED like EntityManager::getReference() does --- .../Persisters/StandardEntityPersister.php | 3 +- lib/Doctrine/ORM/UnitOfWork.php | 3 +- .../ORM/Functional/Ticket/DDC353Test.php | 154 ++++++++++++++++++ .../ORM/Hydration/ObjectHydratorTest.php | 6 +- 4 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC353Test.php diff --git a/lib/Doctrine/ORM/Persisters/StandardEntityPersister.php b/lib/Doctrine/ORM/Persisters/StandardEntityPersister.php index 6065eabe9..6653cac1e 100644 --- a/lib/Doctrine/ORM/Persisters/StandardEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/StandardEntityPersister.php @@ -489,10 +489,11 @@ class StandardEntityPersister } $newData[$field] = $found; } else { + // FIXME: What is happening with subClassees here? $proxy = $this->_em->getProxyFactory()->getProxy($assoc->targetEntityName, $joinColumnValues); $this->_class->reflFields[$field]->setValue($entity, $proxy); $newData[$field] = $proxy; - $this->_em->getUnitOfWork()->addToIdentityMap($proxy); + $this->_em->getUnitOfWork()->registerManaged($proxy, $joinColumnValues, array()); } } } else { diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 911ebaa69..d3ae9d627 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1808,8 +1808,7 @@ class UnitOfWork implements PropertyChangedListener $newValue = $assoc->load($entity, null, $this->_em, $associatedId); } else { $newValue = $this->_em->getProxyFactory()->getProxy($assoc->targetEntityName, $associatedId); - $this->_entityIdentifiers[spl_object_hash($newValue)] = $associatedId; - $this->_identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue; + $this->registerManaged($newValue, $associatedId, array()); } } $this->_originalEntityData[$oid][$field] = $newValue; diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC353Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC353Test.php new file mode 100644 index 000000000..8d645121d --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC353Test.php @@ -0,0 +1,154 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC353File'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC353Picture'), + )); + } catch(\Exception $e) { + + } + } + + public function testWorkingCase() + { + $file = new DDC353File; + + $picture = new DDC353Picture; + $picture->setFile($file); + + $em = $this->_em; + $em->persist($picture); + $em->flush(); + $em->clear(); + + $fileId = $file->getFileId(); + $this->assertTrue($fileId > 0); + + $file = $em->getReference('Doctrine\Tests\ORM\Functional\Ticket\DDC353File', $fileId); + $this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($file), "Reference Proxy should be marked MANAGED."); + + $picture = $em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC353Picture', $picture->getPictureId()); + $this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($picture->getFile()), "Lazy Proxy should be marked MANAGED."); + + $em->remove($picture); + $em->flush(); + } + + public function testFailingCase() + { + $file = new DDC353File; + + $picture = new DDC353Picture; + $picture->setFile($file); + + $em = $this->_em; + $em->persist($picture); + $em->flush(); + $em->clear(); + + $fileId = $file->getFileId(); + $pictureId = $picture->getPictureId(); + + $this->assertTrue($fileId > 0); + + $picture = $em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC353Picture', $pictureId); + $this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($picture->getFile()), "Lazy Proxy should be marked MANAGED."); + + $em->remove($picture); + $em->flush(); + } +} + +/** + * @Entity + */ +class DDC353Picture +{ + /** + * @Column(name="picture_id", type="integer") + * @Id + * @GeneratedValue(strategy="AUTO") + */ + private $pictureId; + + /** + * @ManyToOne(targetEntity="DDC353File", cascade={"persist", "remove"}) + * @JoinColumns({ + * @JoinColumn(name="file_id", referencedColumnName="file_id") + * }) + */ + private $file; + + /** + * Get pictureId + */ + public function getPictureId() + { + return $this->pictureId; + } + + /** + * Set product + */ + public function setProduct($value) + { + $this->product = $value; + } + + /** + * Get product + */ + public function getProduct() + { + return $this->product; + } + + /** + * Set file + */ + public function setFile($value) + { + $this->file = $value; + } + + /** + * Get file + */ + public function getFile() + { + return $this->file; + } +} + +/** + * @Entity + */ +class DDC353File +{ + /** + * @Column(name="file_id", type="integer") + * @Id + * @GeneratedValue(strategy="AUTO") + */ + public $fileId; + + /** + * Get fileId + */ + public function getFileId() + { + return $this->fileId; + } +} diff --git a/tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php b/tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php index f0b6ee50e..734f1f927 100644 --- a/tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php +++ b/tests/Doctrine/Tests/ORM/Hydration/ObjectHydratorTest.php @@ -122,14 +122,16 @@ class ObjectHydratorTest extends HydrationTestCase 'p__shipping_id' => 42 ) ); - + + $proxyInstance = new \Doctrine\Tests\Models\ECommerce\ECommerceShipping(); + // mocking the proxy factory $proxyFactory = $this->getMock('Doctrine\ORM\Proxy\ProxyFactory', array('getProxy'), array(), '', false, false, false); $proxyFactory->expects($this->once()) ->method('getProxy') ->with($this->equalTo('Doctrine\Tests\Models\ECommerce\ECommerceShipping'), array('id' => 42)) - ->will($this->returnValue(new \stdClass)); + ->will($this->returnValue($proxyInstance)); $this->_em->setProxyFactory($proxyFactory);