1
0
mirror of synced 2025-02-02 21:41:45 +03:00

doMerge will mergeEntityStateIntoManagedCopy BEFORE persistNew to let lifecyle events changes be persisted

This commit is contained in:
bilouwan 2016-12-15 12:49:11 +01:00 committed by Marco Pivetta
parent 1d96178097
commit 25efabdb74

View File

@ -1831,6 +1831,7 @@ class UnitOfWork implements PropertyChangedListener
if ( ! $id) { if ( ! $id) {
$managedCopy = $this->newInstance($class); $managedCopy = $this->newInstance($class);
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
$this->persistNew($class, $managedCopy); $this->persistNew($class, $managedCopy);
} else { } else {
$flatId = ($class->containsForeignIdentifier) $flatId = ($class->containsForeignIdentifier)
@ -1862,30 +1863,16 @@ class UnitOfWork implements PropertyChangedListener
$managedCopy = $this->newInstance($class); $managedCopy = $this->newInstance($class);
$class->setIdentifierValues($managedCopy, $id); $class->setIdentifierValues($managedCopy, $id);
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
$this->persistNew($class, $managedCopy); $this->persistNew($class, $managedCopy);
} }else{
} $this->ensureVersionMatch($class, $entity, $managedCopy);
$this->mergeEntityStateIntoManagedCopy($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);
} }
} }
$visited[$oid] = $managedCopy; // mark visited $visited[$oid] = $managedCopy; // mark visited
// $this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
if ($this->isLoaded($entity)) {
if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized()) {
$managedCopy->__load();
}
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
}
if ($class->isChangeTrackingDeferredExplicit()) { if ($class->isChangeTrackingDeferredExplicit()) {
$this->scheduleForDirtyCheck($entity); $this->scheduleForDirtyCheck($entity);
@ -1904,6 +1891,19 @@ class UnitOfWork implements PropertyChangedListener
return $managedCopy; 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 * 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) private function mergeEntityStateIntoManagedCopy($entity, $managedCopy)
{ {
if (!$this->isLoaded($entity)) {
return;
}
if (!$this->isLoaded($managedCopy)) {
$managedCopy->__load();
}
$class = $this->em->getClassMetadata(get_class($entity)); $class = $this->em->getClassMetadata(get_class($entity));
foreach ($this->reflectionPropertiesGetter->getProperties($class->name) as $prop) { foreach ($this->reflectionPropertiesGetter->getProperties($class->name) as $prop) {