From 07016f6da5a301e544627a821ec1523872fe943c Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 11 Oct 2010 20:11:23 +0200 Subject: [PATCH] DDC-834 - Commit fix for requesting references of classes that have subclasses. This is not possible, so we do an eager find instead. Yes this means there is yet another negative performance impact when using Inheritance STI and CTI. --- lib/Doctrine/ORM/EntityManager.php | 12 ++++++--- .../Functional/ClassTableInheritanceTest.php | 25 +++++++++++++++++++ .../Functional/SingleTableInheritanceTest.php | 17 +++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 0cd887fe1..abd89b0d4 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -352,11 +352,15 @@ class EntityManager if ($entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName)) { return $entity; } - if ( ! is_array($identifier)) { - $identifier = array($class->identifier[0] => $identifier); + if ($class->subClasses) { + $entity = $this->find($entityName, $identifier); + } else { + if ( ! is_array($identifier)) { + $identifier = array($class->identifier[0] => $identifier); + } + $entity = $this->proxyFactory->getProxy($class->name, $identifier); + $this->unitOfWork->registerManaged($entity, $identifier, array()); } - $entity = $this->proxyFactory->getProxy($class->name, $identifier); - $this->unitOfWork->registerManaged($entity, $identifier, array()); return $entity; } diff --git a/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php index 24a4e4ad7..072652577 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ClassTableInheritanceTest.php @@ -385,4 +385,29 @@ class ClassTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($manager->getId(), $pmanager->getId()); } + + /** + * @group DDC-834 + */ + public function testGetReferenceEntityWithSubclasses() + { + $manager = new CompanyManager(); + $manager->setName('gblanco'); + $manager->setSalary(1234); + $manager->setTitle('Awesome!'); + $manager->setDepartment('IT'); + + $this->_em->persist($manager); + $this->_em->flush(); + $this->_em->clear(); + + $ref = $this->_em->getReference('Doctrine\Tests\Models\Company\CompanyPerson', $manager->getId()); + $this->assertNotType('Doctrine\ORM\Proxy\Proxy', $ref, "Cannot Request a proxy from a class that has subclasses."); + $this->assertType('Doctrine\Tests\Models\Company\CompanyPerson', $ref); + $this->assertType('Doctrine\Tests\Models\Company\CompanyEmployee', $ref, "Direct fetch of the reference has to load the child class Emplyoee directly."); + $this->_em->clear(); + + $ref = $this->_em->getReference('Doctrine\Tests\Models\Company\CompanyManager', $manager->getId()); + $this->assertType('Doctrine\ORM\Proxy\Proxy', $ref, "A proxy can be generated only if no subclasses exists for the requested reference."); + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/SingleTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/SingleTableInheritanceTest.php index fc303039f..b399092a4 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SingleTableInheritanceTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SingleTableInheritanceTest.php @@ -332,4 +332,21 @@ class SingleTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase $contracts = $repos->findBy(array('salesPerson' => $this->salesPerson->getId())); $this->assertEquals(1, count($contracts), "There should be 1 entities related to " . $this->salesPerson->getId() . " for 'Doctrine\Tests\Models\Company\CompanyFlexUltraContract'"); } + + /** + * @group DDC-834 + */ + public function testGetReferenceEntityWithSubclasses() + { + $this->loadFullFixture(); + + $ref = $this->_em->getReference('Doctrine\Tests\Models\Company\CompanyContract', $this->fix->getId()); + $this->assertNotType('Doctrine\ORM\Proxy\Proxy', $ref, "Cannot Request a proxy from a class that has subclasses."); + $this->assertType('Doctrine\Tests\Models\Company\CompanyContract', $ref); + $this->assertType('Doctrine\Tests\Models\Company\CompanyFixContract', $ref, "Direct fetch of the reference has to load the child class Emplyoee directly."); + $this->_em->clear(); + + $ref = $this->_em->getReference('Doctrine\Tests\Models\Company\CompanyFixContract', $this->fix->getId()); + $this->assertType('Doctrine\ORM\Proxy\Proxy', $ref, "A proxy can be generated only if no subclasses exists for the requested reference."); + } }