diff --git a/UPGRADE.md b/UPGRADE.md index 1594f0c98..1b7e8a90c 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,3 +1,11 @@ +# Upgrade to 2.4 + +## OnFlush and PreFlush event always called + +Before 2.4 the preFlush and onFlush events were only called when there were +actually entities that changed. Now these events are called no matter if there +are entities in the UoW or changes are found. + # Upgrade to 2.3 ## EntityManager#find() not calls EntityRepository#find() anymore diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 99c66472c..24f05748d 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -287,6 +287,9 @@ class UnitOfWork implements PropertyChangedListener $this->collectionUpdates || $this->collectionDeletions || $this->orphanRemovals)) { + $this->dispatchOnFlushEvent(); + $this->dispatchPostFlushEvent(); + return; // Nothing to do. } @@ -296,10 +299,7 @@ class UnitOfWork implements PropertyChangedListener } } - // Raise onFlush - if ($this->evm->hasListeners(Events::onFlush)) { - $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em)); - } + $this->dispatchOnFlushEvent(); // Now we need a commit order to maintain referential integrity $commitOrder = $this->getCommitOrder(); @@ -354,10 +354,7 @@ class UnitOfWork implements PropertyChangedListener $coll->takeSnapshot(); } - // Raise postFlush - if ($this->evm->hasListeners(Events::postFlush)) { - $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->em)); - } + $this->dispatchPostFlushEvent(); // Clear up $this->entityInsertions = @@ -3153,4 +3150,18 @@ class UnitOfWork implements PropertyChangedListener return isset($this->readOnlyObjects[spl_object_hash($object)]); } + + private function dispatchOnFlushEvent() + { + if ($this->evm->hasListeners(Events::onFlush)) { + $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em)); + } + } + + private function dispatchPostFlushEvent() + { + if ($this->evm->hasListeners(Events::postFlush)) { + $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->em)); + } + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php b/tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php index 8167e8587..156e89b0e 100644 --- a/tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php @@ -48,6 +48,26 @@ class FlushEventTest extends \Doctrine\Tests\OrmFunctionalTestCase //echo "SECOND FLUSH"; //$this->_em->flush(); } + + /** + * @group DDC-2173 + */ + public function testPreAndOnFlushCalledAlways() + { + $listener = new OnFlushCalledListener(); + $this->_em->getEventManager()->addEventListener(Events::onFlush, $listener); + $this->_em->getEventManager()->addEventListener(Events::preFlush, $listener); + + $this->_em->flush(); + + $this->assertEquals(1, $listener->preFlush); + $this->assertEquals(1, $listener->onFlush); + + $this->_em->flush(); + + $this->assertEquals(2, $listener->preFlush); + $this->assertEquals(2, $listener->onFlush); + } } class OnFlushListener @@ -91,4 +111,18 @@ class OnFlushListener } } +class OnFlushCalledListener +{ + public $preFlush = 0; + public $onFlush = 0; + public function preFlush($args) + { + $this->preFlush++; + } + + public function onFlush($args) + { + $this->onFlush++; + } +}