Skip to content

Commit

Permalink
Fixing doctrine#3037 / DDC-2332
Browse files Browse the repository at this point in the history
  • Loading branch information
sandvige authored Sep 26, 2018
1 parent a72bdc6 commit d32ce7f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ class UnitOfWork implements PropertyChangedListener
*/
private $identityMap = array();

/**
* Associate entities with OIDs to ensure PHP won't recycle a managed entity
*
* DDC-2332 / #3037
*
* @var array
*/
private $oidMap = array();

/**
* Map of all identifiers of managed entities.
* Keys are object ids (spl_object_hash).
Expand Down Expand Up @@ -1406,14 +1415,16 @@ public function isEntityScheduled($entity)
public function addToIdentityMap($entity)
{
$classMetadata = $this->em->getClassMetadata(get_class($entity));
$idHash = implode(' ', $this->entityIdentifiers[spl_object_hash($entity)]);
$oid = spl_object_hash($entity);
$idHash = implode(' ', $this->entityIdentifiers[$oid]);

if ($idHash === '') {
throw ORMInvalidArgumentException::entityWithoutIdentity($classMetadata->name, $entity);
}

$className = $classMetadata->rootEntityName;

$this->oidMap[$oid] = $entity;
if (isset($this->identityMap[$className][$idHash])) {
return false;
}
Expand Down Expand Up @@ -1529,9 +1540,10 @@ public function removeFromIdentityMap($entity)

$className = $classMetadata->rootEntityName;

unset($this->oidMap[$oid]);
unset($this->readOnlyObjects[$oid]);
if (isset($this->identityMap[$className][$idHash])) {
unset($this->identityMap[$className][$idHash]);
unset($this->readOnlyObjects[$oid]);

//$this->entityStates[$oid] = self::STATE_DETACHED;

Expand Down Expand Up @@ -2397,6 +2409,7 @@ public function clear($entityName = null)
{
if ($entityName === null) {
$this->identityMap =
$this->oidMap =
$this->entityIdentifiers =
$this->originalEntityData =
$this->entityChangeSets =
Expand Down Expand Up @@ -2568,6 +2581,7 @@ public function createEntity($className, array $data, &$hints = array())
$this->entityStates[$oid] = self::STATE_MANAGED;
$this->originalEntityData[$oid] = $data;

$this->oidMap[$oid] = $entity;
$this->identityMap[$class->rootEntityName][$idHash] = $entity;

if ($entity instanceof NotifyPropertyChanged) {
Expand Down Expand Up @@ -2725,6 +2739,7 @@ public function createEntity($className, array $data, &$hints = array())
$newValueOid = spl_object_hash($newValue);
$this->entityIdentifiers[$newValueOid] = $associatedId;
$this->identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue;
$this->oidMap[$newValueOid] = $newValue;

if (
$newValue instanceof NotifyPropertyChanged &&
Expand Down

0 comments on commit d32ce7f

Please sign in to comment.