From a5cf6417b3048aa6e1336e496556de453580b25f Mon Sep 17 00:00:00 2001 From: Andreas Flack Date: Thu, 12 Jun 2014 17:28:13 +0200 Subject: [PATCH 1/3] Add failing test for DDC-3160 --- .../ORM/Functional/Ticket/DDC3160Test.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php new file mode 100644 index 000000000..6fe88e1ae --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php @@ -0,0 +1,70 @@ +useModelSet('cms'); + parent::setUp(); + } + + /** + * @group DDC-3160 + */ + public function testNoUpdateOnInsert() + { + $listener = new OnFlushListener(); + $this->_em->getEventManager()->addEventListener(Events::onFlush, $listener); + + $user = new CmsUser; + $user->username = 'romanb'; + $user->name = 'Roman'; + $user->status = 'Dev'; + + $this->_em->persist($user); + $this->_em->flush(); + + $this->_em->refresh($user); + + $this->assertEquals('romanc', $user->username); + $this->assertEquals(1, $listener->inserts); + $this->assertEquals(0, $listener->updates); + } +} + +class OnFlushListener +{ + public $inserts = 0; + public $updates = 0; + + public function onFlush(OnFlushEventArgs $args) + { + $em = $args->getEntityManager(); + $uow = $em->getUnitOfWork(); + + foreach ($uow->getScheduledEntityInsertions() as $entity) { + $this->inserts++; + if ($entity instanceof CmsUser) { + $entity->username = 'romanc'; + $cm = $em->getClassMetadata(get_class($entity)); + $uow->recomputeSingleEntityChangeSet($cm, $entity); + } + } + + foreach ($uow->getScheduledEntityUpdates() as $entity) { + $this->updates++; + } + } +} + From 65e7cc9143e818670cae6a7167c3c70736fa870a Mon Sep 17 00:00:00 2001 From: Justin Zimmerman Date: Thu, 26 Jun 2014 17:15:57 -0400 Subject: [PATCH 2/3] [DDC-3160] Change to fix that was implemented for DDC-2996. A fix for DDC-2996 was implemented that broke quite a few extensions. This commit is an attempt to fix the DDC-2996 bug without the adverse side effects seen in DDC-3160. Basically, if changes are detected that would cause a changeset to be made, but the entity is awaiting insertion, the code will not save the changeset nor flag the entity as awaiting updating in the Unit of Work. Some styling tweaks based on Pull Request guidelines. --- lib/Doctrine/ORM/UnitOfWork.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index c74329581..61a3c7ecc 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -943,12 +943,13 @@ class UnitOfWork implements PropertyChangedListener } if ($changeSet) { - $this->entityChangeSets[$oid] = (isset($this->entityChangeSets[$oid])) - ? array_merge($this->entityChangeSets[$oid], $changeSet) - : $changeSet; - + if (isset($this->entityChangeSets[$oid])) { + $this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet); + } else if ( ! isset($this->entityInsertions[$oid])) { + $this->entityChangeSets[$oid] = $changeSet; + $this->entityUpdates[$oid] = $entity; + } $this->originalEntityData[$oid] = $actualData; - $this->entityUpdates[$oid] = $entity; } } @@ -2405,12 +2406,12 @@ class UnitOfWork implements PropertyChangedListener } } else { $visited = array(); - + foreach ($this->identityMap as $className => $entities) { if ($className !== $entityName) { continue; } - + foreach ($entities as $entity) { $this->doDetach($entity, $visited, false); } @@ -2522,7 +2523,7 @@ class UnitOfWork implements PropertyChangedListener $id = array($class->identifier[0] => $id); } - + $idHash = implode(' ', $id); if (isset($this->identityMap[$class->rootEntityName][$idHash])) { From 6a4867512e0d361ad6894a286cb7803682154616 Mon Sep 17 00:00:00 2001 From: Justin Zimmerman Date: Fri, 4 Jul 2014 11:05:04 -0400 Subject: [PATCH 3/3] Fix test issues. --- .../Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php index 6fe88e1ae..c0ed69dd0 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php @@ -3,16 +3,16 @@ namespace Doctrine\Tests\ORM\Functional\Ticket; use Doctrine\Tests\Models\CMS\CmsUser; -use Doctrine\Tests\Models\CMS\CmsPhonenumber; use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\Events; +use Doctrine\Tests\OrmFunctionalTestCase; /** * FlushEventTest * * @author robo */ -class FlushEventTest extends \Doctrine\Tests\OrmFunctionalTestCase +class DDC3160Test extends OrmFunctionalTestCase { protected function setUp() { $this->useModelSet('cms'); @@ -24,7 +24,7 @@ class FlushEventTest extends \Doctrine\Tests\OrmFunctionalTestCase */ public function testNoUpdateOnInsert() { - $listener = new OnFlushListener(); + $listener = new DDC3160OnFlushListener(); $this->_em->getEventManager()->addEventListener(Events::onFlush, $listener); $user = new CmsUser; @@ -43,7 +43,7 @@ class FlushEventTest extends \Doctrine\Tests\OrmFunctionalTestCase } } -class OnFlushListener +class DDC3160OnFlushListener { public $inserts = 0; public $updates = 0;