diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index cdd178baf..2839099fc 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -355,7 +355,7 @@ class EntityManager implements ObjectManager // Check identity map first, if its already in there just return it. if ($entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName)) { - return $entity; + return ($entity instanceof $class->name) ? $entity : null; } if ($class->subClasses) { $entity = $this->find($entityName, $identifier); @@ -395,7 +395,7 @@ class EntityManager implements ObjectManager // Check identity map first, if its already in there just return it. if ($entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName)) { - return $entity; + return ($entity instanceof $class->name) ? $entity : null; } if ( ! is_array($identifier)) { $identifier = array($class->identifier[0] => $identifier); diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 60fc91760..a92ce7355 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -98,6 +98,10 @@ class EntityRepository implements ObjectRepository { // Check identity map first if ($entity = $this->_em->getUnitOfWork()->tryGetById($id, $this->_class->rootEntityName)) { + if (!($entity instanceof $this->_class->name)) { + return null; + } + if ($lockMode != LockMode::NONE) { $this->_em->lock($entity, $lockMode, $lockVersion); } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1041Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1041Test.php new file mode 100644 index 000000000..cf5a5f223 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1041Test.php @@ -0,0 +1,33 @@ +useModelSet('company'); + parent::setUp(); + } + + public function testGrabWrongSubtypeReturnsNull() + { + $fix = new \Doctrine\Tests\Models\Company\CompanyFixContract(); + $fix->setFixPrice(2000); + + $this->_em->persist($fix); + $this->_em->flush(); + + $id = $fix->getId(); + + $this->assertNull($this->_em->find('Doctrine\Tests\Models\Company\CompanyFlexContract', $id)); + $this->assertNull($this->_em->getReference('Doctrine\Tests\Models\Company\CompanyFlexContract', $id)); + $this->assertNull($this->_em->getPartialReference('Doctrine\Tests\Models\Company\CompanyFlexContract', $id)); + } +} \ No newline at end of file