diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index a237968d89d..681c8d28867 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -328,7 +328,7 @@ public function commit($entity = null) } // Compute changes done since last commit. - if ($entity === null) { + if (null === $entity) { $this->computeChangeSets(); } elseif (is_object($entity)) { $this->computeSingleEntityChangeSet($entity); @@ -418,17 +418,40 @@ public function commit($entity = null) $this->dispatchPostFlushEvent(); + $this->postCommitClear($entity); + } + + /** + * @param null|object|array $entity + */ + private function postCommitClear($entity = null) + { + // Clear up $this->entityInsertions = $this->entityUpdates = $this->entityDeletions = $this->extraUpdates = - $this->entityChangeSets = $this->collectionUpdates = $this->collectionDeletions = $this->visitedCollections = - $this->scheduledForSynchronization = $this->orphanRemovals = array(); + + if (null === $entity) { + $this->entityChangeSets = $this->scheduledForSynchronization = array(); + return; + } + + if (is_object($entity)) { + $entity = [$entity]; + } + + foreach ($entity as $object) { + $oid = spl_object_hash($object); + $class = $this->em->getClassMetadata(get_class($object)); + $this->clearEntityChangeSet($oid); + $this->clearScheduledForSynchronization($class, $oid); + } } /** @@ -3111,7 +3134,16 @@ public function registerManaged($entity, array $id, array $data) */ public function clearEntityChangeSet($oid) { - $this->entityChangeSets[$oid] = array(); + unset($this->entityChangeSets[$oid]); + } + + /** + * @param $class + * @param string $oid + */ + public function clearScheduledForSynchronization($class, $oid) + { + unset($this->scheduledForSynchronization[$class->rootEntityName][$oid]); } /* PropertyChangedListener implementation */ diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index d855c7817b0..1706f913c00 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -150,7 +150,29 @@ public function testChangeTrackingNotify() $this->_unitOfWork->persist($entity); $this->_unitOfWork->commit(); - $this->assertEquals(1, count($persister->getInserts())); + $this->assertCount(1, $persister->getInserts()); + + // Create and Set first entity + $entity1 = new NotifyChangedEntity; + $entity1->setData('thedata'); + $this->_unitOfWork->persist($entity1); + + // Create and Set second entity + $entity2 = new NotifyChangedEntity; + $entity2->setData('thedata'); + $this->_unitOfWork->persist($entity2); + + $this->_unitOfWork->commit($entity1); + $this->assertCount(1, $this->_unitOfWork->getEntityChangeSet($entity2)); + + // Create and Set third entity + $entity3 = new NotifyChangedEntity; + $entity3->setData('thedata'); + $this->_unitOfWork->persist($entity3); + + $this->_unitOfWork->commit([$entity1,$entity2]); + $this->assertCount(1, $this->_unitOfWork->getEntityChangeSet($entity3)); + $persister->reset(); $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity));