DDC-1519 - Fix bug in merging of entities that contain foreign identifiers
This commit is contained in:
parent
41a3d90a57
commit
e8a47b3921
@ -1647,7 +1647,20 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
|
|
||||||
$this->persistNew($class, $managedCopy);
|
$this->persistNew($class, $managedCopy);
|
||||||
} else {
|
} 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) {
|
if ($managedCopy) {
|
||||||
// We have the entity in-memory already, just make sure its not removed.
|
// We have the entity in-memory already, just make sure its not removed.
|
||||||
@ -1657,7 +1670,7 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// We need to fetch the managed copy in order to merge.
|
// 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) {
|
if ($managedCopy === null) {
|
||||||
|
@ -417,4 +417,20 @@ class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
|
|
||||||
return $this->_em->find(get_class($editor), $editor->id);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user