From 25efabdb7495585a772aaf3b38f34c047ec8c578 Mon Sep 17 00:00:00 2001 From: bilouwan Date: Thu, 15 Dec 2016 12:49:11 +0100 Subject: [PATCH] doMerge will mergeEntityStateIntoManagedCopy BEFORE persistNew to let lifecyle events changes be persisted --- lib/Doctrine/ORM/UnitOfWork.php | 46 +++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index ed7e52e69..ac1251425 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1831,6 +1831,7 @@ class UnitOfWork implements PropertyChangedListener if ( ! $id) { $managedCopy = $this->newInstance($class); + $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy); $this->persistNew($class, $managedCopy); } else { $flatId = ($class->containsForeignIdentifier) @@ -1862,30 +1863,16 @@ class UnitOfWork implements PropertyChangedListener $managedCopy = $this->newInstance($class); $class->setIdentifierValues($managedCopy, $id); + $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy); $this->persistNew($class, $managedCopy); - } - } - - if ($class->isVersioned && $this->isLoaded($managedCopy) && $this->isLoaded($entity)) { - $reflField = $class->reflFields[$class->versionField]; - $managedCopyVersion = $reflField->getValue($managedCopy); - $entityVersion = $reflField->getValue($entity); - - // Throw exception if versions don't match. - if ($managedCopyVersion != $entityVersion) { - throw OptimisticLockException::lockFailedVersionMismatch($entity, $entityVersion, $managedCopyVersion); + }else{ + $this->ensureVersionMatch($class, $entity, $managedCopy); + $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy); } } $visited[$oid] = $managedCopy; // mark visited - - if ($this->isLoaded($entity)) { - if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized()) { - $managedCopy->__load(); - } - - $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy); - } + // $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy); if ($class->isChangeTrackingDeferredExplicit()) { $this->scheduleForDirtyCheck($entity); @@ -1904,6 +1891,19 @@ class UnitOfWork implements PropertyChangedListener return $managedCopy; } + private function ensureVersionMatch(ClassMetadata $class, $entity, $managedCopy) { + if ($class->isVersioned && $this->isLoaded($managedCopy) && $this->isLoaded($entity)) { + $reflField = $class->reflFields[$class->versionField]; + $managedCopyVersion = $reflField->getValue($managedCopy); + $entityVersion = $reflField->getValue($entity); + + // Throw exception if versions don't match. + if ($managedCopyVersion != $entityVersion) { + throw OptimisticLockException::lockFailedVersionMismatch($entity, $entityVersion, $managedCopyVersion); + } + } + } + /** * Tests if an entity is loaded - must either be a loaded proxy or not a proxy * @@ -3356,6 +3356,14 @@ class UnitOfWork implements PropertyChangedListener */ private function mergeEntityStateIntoManagedCopy($entity, $managedCopy) { + if (!$this->isLoaded($entity)) { + return; + } + + if (!$this->isLoaded($managedCopy)) { + $managedCopy->__load(); + } + $class = $this->em->getClassMetadata(get_class($entity)); foreach ($this->reflectionPropertiesGetter->getProperties($class->name) as $prop) {