Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging a new entity with PrePersist event make changes in callback not be considered #6174

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,7 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, arra
if ( ! $id) {
$managedCopy = $this->newInstance($class);

$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
$this->persistNew($class, $managedCopy);
} else {
$flatId = ($class->containsForeignIdentifier)
Expand Down Expand Up @@ -1847,30 +1848,16 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, arra
$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);
Expand All @@ -1889,6 +1876,19 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, arra
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
*
Expand Down Expand Up @@ -3338,6 +3338,14 @@ private function isIdentifierEquals($entity1, $entity2)
*/
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) {
Expand Down
29 changes: 25 additions & 4 deletions tests/Doctrine/Tests/Models/Company/CompanyContractListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

class CompanyContractListener
{
const PRE_PERSIST = 0;

public $postPersistCalls;
public $prePersistCalls;

public $postUpdateCalls;
public $preUpdateCalls;

public $postRemoveCalls;
public $preRemoveCalls;

public $preFlushCalls;

public $postLoadCalls;


public $snapshots = [];

/**
* @PostPersist
*/
Expand All @@ -30,6 +34,7 @@ public function postPersistHandler(CompanyContract $contract)
*/
public function prePersistHandler(CompanyContract $contract)
{
$this->snapshots[self::PRE_PERSIST][] = $this->takeSnapshot($contract);
$this->prePersistCalls[] = func_get_args();
}

Expand Down Expand Up @@ -81,4 +86,20 @@ public function postLoadHandler(CompanyContract $contract)
$this->postLoadCalls[] = func_get_args();
}

public function takeSnapshot(CompanyContract $contract)
{
$snapshot = [];
$reflexion = new \ReflectionClass($contract);
foreach ($reflexion->getProperties() as $property) {
$property->setAccessible(true);
$value = $property->getValue($contract);
if (is_object($value) || is_array($value)) {
continue;
}
$snapshot[$property->getName()] = $property->getValue($contract);
}

return $snapshot;
}

}
96 changes: 96 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EntityListenersOnMergeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\Company\CompanyContractListener;
use Doctrine\Tests\Models\Company\CompanyFixContract;
use Doctrine\Tests\Models\DDC3597\DDC3597Image;
use Doctrine\Tests\Models\DDC3597\DDC3597Media;
use Doctrine\Tests\Models\DDC3597\DDC3597Root;

/**
* @group DDC-1955
*/
class EntityListenersOnMergeTest extends \Doctrine\Tests\OrmFunctionalTestCase
{

/**
* @var \Doctrine\Tests\Models\Company\CompanyContractListener
*/
private $listener;

protected function setUp()
{
$this->useModelSet('company');
parent::setUp();

$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(DDC3597Root::class),
$this->_em->getClassMetadata(DDC3597Media::class),
$this->_em->getClassMetadata(DDC3597Image::class),
]
);

$this->listener = $this->_em->getConfiguration()
->getEntityListenerResolver()
->resolve('Doctrine\Tests\Models\Company\CompanyContractListener');
}

protected function tearDown()
{
parent::tearDown();
$this->_schemaTool->dropSchema(
[
$this->_em->getClassMetadata(DDC3597Root::class),
$this->_em->getClassMetadata(DDC3597Media::class),
$this->_em->getClassMetadata(DDC3597Image::class),
]
);
}

public function testMergeNewEntityLifecyleEventsModificationsShouldBeKept()
{
$imageEntity = new DDC3597Image('foobar');
$imageEntity->setFormat('JPG');
$imageEntity->setSize(123);
$imageEntity->getDimension()->setWidth(300);
$imageEntity->getDimension()->setHeight(500);

$imageEntity = $this->_em->merge($imageEntity);

$this->assertNotNull($imageEntity->getCreatedAt());
$this->assertNotNull($imageEntity->getUpdatedAt());
}

public function testPrePersistListenersShouldBeFiredWithCorrectEntityData()
{
$fix = new CompanyFixContract();
$fix->setFixPrice(2000);

$this->listener->prePersistCalls = [];

$fix = $this->_em->merge($fix);
$this->_em->flush();

$this->assertCount(1, $this->listener->prePersistCalls);

$this->assertSame($fix, $this->listener->prePersistCalls[0][0]);

$this->assertInstanceOf(
'Doctrine\Tests\Models\Company\CompanyFixContract',
$this->listener->prePersistCalls[0][0]
);

$this->assertInstanceOf(
'Doctrine\ORM\Event\LifecycleEventArgs',
$this->listener->prePersistCalls[0][1]
);

$this->assertArrayHasKey('fixPrice', $this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]);
$this->assertEquals(
$fix->getFixPrice(),
$this->listener->snapshots[CompanyContractListener::PRE_PERSIST][0]['fixPrice']
);
}
}