From cd746beae2a30b4413aadca9005b9815273231f2 Mon Sep 17 00:00:00 2001 From: Rico Humme Date: Fri, 3 Jun 2016 13:48:05 +0200 Subject: [PATCH 1/9] Clear entityInsertions for specific entityName --- lib/Doctrine/ORM/UnitOfWork.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index d27748a99..3f0b6f7a0 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2418,6 +2418,13 @@ class UnitOfWork implements PropertyChangedListener $this->doDetach($entity, $visited, false); } } + + foreach ($this->entityInsertions as $hash => $entity) { + if (get_class($entity) != $entityName) { + continue; + } + unset($this->entityInsertions[$hash]); + } } if ($this->evm->hasListeners(Events::onClear)) { From 996c5048abdf3138515912c4860e708eb00d5e40 Mon Sep 17 00:00:00 2001 From: Rico Humme Date: Fri, 3 Jun 2016 16:13:52 +0200 Subject: [PATCH 2/9] Test Case for Clear entityInsertions for specific entityName --- tests/Doctrine/Tests/ORM/UnitOfWorkTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index d855c7817..b24598c81 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -321,6 +321,27 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity)); } + public function testPersistedEntityAndClearManager() + { + $entity1 = new ForumUser(); + $entity1->id = 123; + + $entity2 = new ForumAvatar(); + $entity2->id = 456; + + $this->_unitOfWork->persist($entity1); + $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity1)); + + $this->_unitOfWork->persist($entity2); + $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity2)); + + $this->_unitOfWork->clear(ForumAvatar::class); + $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity1)); + $this->assertFalse($this->_unitOfWork->isInIdentityMap($entity2)); + $this->assertTrue($this->_unitOfWork->isScheduledForInsert($entity1)); + $this->assertFalse($this->_unitOfWork->isScheduledForInsert($entity2)); + } + /** * Data Provider * From 110d771883bfdb215142b191243f5bf45cfe35cf Mon Sep 17 00:00:00 2001 From: Rico Humme Date: Fri, 3 Jun 2016 16:27:27 +0200 Subject: [PATCH 3/9] Split of functionality in separate functions --- lib/Doctrine/ORM/UnitOfWork.php | 50 +++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 3f0b6f7a0..1b7425d74 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2407,24 +2407,8 @@ class UnitOfWork implements PropertyChangedListener $this->commitOrderCalculator->clear(); } } else { - $visited = array(); - - foreach ($this->identityMap as $className => $entities) { - if ($className !== $entityName) { - continue; - } - - foreach ($entities as $entity) { - $this->doDetach($entity, $visited, false); - } - } - - foreach ($this->entityInsertions as $hash => $entity) { - if (get_class($entity) != $entityName) { - continue; - } - unset($this->entityInsertions[$hash]); - } + $this->clearIdentityMap($entityName); + $this->clearIdentityInsertions($entityName); } if ($this->evm->hasListeners(Events::onClear)) { @@ -3478,4 +3462,34 @@ class UnitOfWork implements PropertyChangedListener { $this->hydrationCompleteHandler->hydrationComplete(); } + + /** + * @param $entityName + */ + private function clearIdentityMap($entityName) + { + $visited = array(); + + foreach ($this->identityMap as $className => $entities) { + if ($className !== $entityName) { + continue; + } + + foreach ($entities as $entity) { + $this->doDetach($entity, $visited, false); + } + } + } + + /** + * @param $entityName + */ + private function clearIdentityInsertions($entityName) + { + foreach ($this->entityInsertions as $hash => $entity) { + if (get_class($entity) === $entityName) { + unset($this->entityInsertions[$hash]); + } + } + } } From 4a38c96ec57e3c860c2e0c85cd98f09e685f398c Mon Sep 17 00:00:00 2001 From: Rico Humme Date: Fri, 3 Jun 2016 16:31:35 +0200 Subject: [PATCH 4/9] Correct naming convention of function. Was confusing otherwise --- lib/Doctrine/ORM/UnitOfWork.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 1b7425d74..1547ab72a 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2408,7 +2408,7 @@ class UnitOfWork implements PropertyChangedListener } } else { $this->clearIdentityMap($entityName); - $this->clearIdentityInsertions($entityName); + $this->clearEntityInsertions($entityName); } if ($this->evm->hasListeners(Events::onClear)) { @@ -3484,7 +3484,7 @@ class UnitOfWork implements PropertyChangedListener /** * @param $entityName */ - private function clearIdentityInsertions($entityName) + private function clearEntityInsertions($entityName) { foreach ($this->entityInsertions as $hash => $entity) { if (get_class($entity) === $entityName) { From 7fbcbfa271612fbacdd6a2e23b9648270c20cdd0 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 5 Jun 2016 23:54:16 +0200 Subject: [PATCH 5/9] #5849 #5850 adding group annotations to the newly introduced test case --- tests/Doctrine/Tests/ORM/UnitOfWorkTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index b24598c81..1e9f0f33c 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -321,6 +321,10 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity)); } + /** + * @group 5849 + * @group 5850 + */ public function testPersistedEntityAndClearManager() { $entity1 = new ForumUser(); From 7378035f68696b28e95f1d98b82d2b28f8e064c5 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 6 Jun 2016 00:08:26 +0200 Subject: [PATCH 6/9] #5849 #5850 correcting test scenario: identity map could not be built with auto-generated identities+persist --- tests/Doctrine/Tests/ORM/UnitOfWorkTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index 1e9f0f33c..2e0233ba2 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -14,6 +14,9 @@ use Doctrine\Tests\Mocks\EntityPersisterMock; use Doctrine\Tests\Mocks\UnitOfWorkMock; use Doctrine\Tests\Models\Forum\ForumAvatar; use Doctrine\Tests\Models\Forum\ForumUser; +use Doctrine\Tests\Models\GeoNames\City; +use Doctrine\Tests\Models\GeoNames\Country; +use Doctrine\Tests\OrmTestCase; use stdClass; /** @@ -327,11 +330,8 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase */ public function testPersistedEntityAndClearManager() { - $entity1 = new ForumUser(); - $entity1->id = 123; - - $entity2 = new ForumAvatar(); - $entity2->id = 456; + $entity1 = new City(123, 'London'); + $entity2 = new Country(456, 'United Kingdom'); $this->_unitOfWork->persist($entity1); $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity1)); @@ -339,7 +339,7 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase $this->_unitOfWork->persist($entity2); $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity2)); - $this->_unitOfWork->clear(ForumAvatar::class); + $this->_unitOfWork->clear(Country::class); $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity1)); $this->assertFalse($this->_unitOfWork->isInIdentityMap($entity2)); $this->assertTrue($this->_unitOfWork->isScheduledForInsert($entity1)); From ec4dd4ab441bbff8b2cbfca65ae1d826fedd62af Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 6 Jun 2016 00:10:18 +0200 Subject: [PATCH 7/9] #5849 #5850 renamed `clearIdentityMap` to `clearIdentityMapForEntityName`, for clarity --- lib/Doctrine/ORM/UnitOfWork.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 1547ab72a..ee499a1a8 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2407,7 +2407,7 @@ class UnitOfWork implements PropertyChangedListener $this->commitOrderCalculator->clear(); } } else { - $this->clearIdentityMap($entityName); + $this->clearIdentityMapForEntityName($entityName); $this->clearEntityInsertions($entityName); } @@ -3464,9 +3464,9 @@ class UnitOfWork implements PropertyChangedListener } /** - * @param $entityName + * @param string $entityName */ - private function clearIdentityMap($entityName) + private function clearIdentityMapForEntityName($entityName) { $visited = array(); From 800215040a31f0f03e73d09cfcbdd9e81f8182a5 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 6 Jun 2016 00:11:19 +0200 Subject: [PATCH 8/9] #5849 #5850 refactored `clearIdentityMapForEntityName` to remove useless looping --- lib/Doctrine/ORM/UnitOfWork.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index ee499a1a8..ab91b8bab 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -3468,16 +3468,14 @@ class UnitOfWork implements PropertyChangedListener */ private function clearIdentityMapForEntityName($entityName) { - $visited = array(); + if (! isset($this->identityMap[$entityName])) { + return; + } - foreach ($this->identityMap as $className => $entities) { - if ($className !== $entityName) { - continue; - } + $visited = []; - foreach ($entities as $entity) { - $this->doDetach($entity, $visited, false); - } + foreach ($this->identityMap[$entityName] as $entity) { + $this->doDetach($entity, $visited, false); } } From fecadf059c21eecac732d5acb031c2fbcf28b496 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 6 Jun 2016 00:13:39 +0200 Subject: [PATCH 9/9] #5849 #5850 renamed `clearEntityInsertions` to `clearEntityInsertionsForEntityName`, for clarity --- lib/Doctrine/ORM/UnitOfWork.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index ab91b8bab..c2f037e2d 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2408,7 +2408,7 @@ class UnitOfWork implements PropertyChangedListener } } else { $this->clearIdentityMapForEntityName($entityName); - $this->clearEntityInsertions($entityName); + $this->clearEntityInsertionsForEntityName($entityName); } if ($this->evm->hasListeners(Events::onClear)) { @@ -3480,9 +3480,9 @@ class UnitOfWork implements PropertyChangedListener } /** - * @param $entityName + * @param string $entityName */ - private function clearEntityInsertions($entityName) + private function clearEntityInsertionsForEntityName($entityName) { foreach ($this->entityInsertions as $hash => $entity) { if (get_class($entity) === $entityName) {