[DDC-2246] Fix bug with UnitOfWork#getEntityState() and entities with foreign identifier.
This commit is contained in:
parent
916424af49
commit
5298c03fce
@ -1401,6 +1401,10 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
return self::STATE_NEW;
|
return self::STATE_NEW;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($class->containsForeignIdentifier) {
|
||||||
|
$id = $this->flattenIdentifier($class, $id);
|
||||||
|
}
|
||||||
|
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case ($class->isIdentifierNatural());
|
case ($class->isIdentifierNatural());
|
||||||
// Check for a version field, if available, to avoid a db lookup.
|
// Check for a version field, if available, to avoid a db lookup.
|
||||||
@ -1725,6 +1729,29 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
return $this->doMerge($entity, $visited);
|
return $this->doMerge($entity, $visited);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* convert foreign identifiers into scalar foreign key values to avoid object to string conversion failures.
|
||||||
|
*
|
||||||
|
* @param ClassMetadata $class
|
||||||
|
* @param array $id
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function flattenIdentifier($class, $id)
|
||||||
|
{
|
||||||
|
$flatId = array();
|
||||||
|
|
||||||
|
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]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $flatId;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes a merge operation on an entity.
|
* Executes a merge operation on an entity.
|
||||||
*
|
*
|
||||||
@ -1772,19 +1799,9 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
|
|
||||||
$this->persistNew($class, $managedCopy);
|
$this->persistNew($class, $managedCopy);
|
||||||
} else {
|
} else {
|
||||||
$flatId = $id;
|
$flatId = ($class->containsForeignIdentifier)
|
||||||
if ($class->containsForeignIdentifier) {
|
? $this->flattenIdentifier($class, $id)
|
||||||
// convert foreign identifiers into scalar foreign key
|
: $id;
|
||||||
// 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);
|
$managedCopy = $this->tryGetById($flatId, $class->rootEntityName);
|
||||||
|
|
||||||
|
@ -461,4 +461,22 @@ class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
|
|
||||||
$this->assertEquals($before + 3, count($data));
|
$this->assertEquals($before + 3, count($data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-2246
|
||||||
|
*/
|
||||||
|
public function testGetEntityState()
|
||||||
|
{
|
||||||
|
$this->article1 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id());
|
||||||
|
$this->article2 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article2->id());
|
||||||
|
|
||||||
|
$this->reference = new DDC117Reference($this->article2, $this->article1, "Test-Description");
|
||||||
|
|
||||||
|
$this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($this->reference));
|
||||||
|
|
||||||
|
$idCriteria = array('source' => $this->article1->id(), 'target' => $this->article2->id());
|
||||||
|
$reference = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Reference", $idCriteria);
|
||||||
|
|
||||||
|
$this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($reference));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user