diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 1b0619efe..17cc29807 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -117,7 +117,7 @@ class EntityRepository implements ObjectRepository } $sortedId[$identifier] = $id[$identifier]; } - + // Check identity map first if ($entity = $this->_em->getUnitOfWork()->tryGetById($sortedId, $this->_class->rootEntityName)) { if ( ! ($entity instanceof $this->_class->name)) { diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index f02083d0d..a026d001a 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1647,7 +1647,20 @@ class UnitOfWork implements PropertyChangedListener $this->persistNew($class, $managedCopy); } else { - $managedCopy = $this->tryGetById($id, $class->rootEntityName); + $flatId = $id; + if ($class->containsForeignIdentifier) { + // convert foreign identifiers into scalar foreign key + // values to avoid object to string conversion failures. + foreach ($id as $idField => $idValue) { + if (isset($class->associationMappings[$idField])) { + $targetClassMetadata = $this->em->getClassMetadata($class->associationMappings[$idField]['targetEntity']); + $associatedId = $this->getEntityIdentifier($idValue); + $flatId[$idField] = $associatedId[$targetClassMetadata->identifier[0]]; + } + } + } + + $managedCopy = $this->tryGetById($flatId, $class->rootEntityName); if ($managedCopy) { // We have the entity in-memory already, just make sure its not removed. @@ -1657,7 +1670,7 @@ class UnitOfWork implements PropertyChangedListener } } else { // We need to fetch the managed copy in order to merge. - $managedCopy = $this->em->find($class->name, $id); + $managedCopy = $this->em->find($class->name, $flatId); } if ($managedCopy === null) { diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC117Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC117Test.php index 9e1b5b074..c79bd2f46 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC117Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC117Test.php @@ -417,4 +417,20 @@ class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase return $this->_em->find(get_class($editor), $editor->id); } + + /** + * @group DDC-1519 + */ + public function testMergeForeignKeyIdentifierEntity() + { + $idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id()); + + $refRep = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria); + + $this->_em->detach($refRep); + $refRep = $this->_em->merge($refRep); + + $this->assertEquals($this->article1->id(), $refRep->source()->id()); + $this->assertEquals($this->article2->id(), $refRep->target()->id()); + } }