From 9322ca7052bd08bc7315627de74efb1f0aa06a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesc=20Ros=C3=A0s?= Date: Wed, 11 Jul 2012 00:21:13 +0200 Subject: [PATCH 1/3] Ensure onFlush and postFlush events are always raised --- lib/Doctrine/ORM/UnitOfWork.php | 103 ++++++++++++++++---------------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 99c66472c..6274ec6d4 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -281,16 +281,15 @@ class UnitOfWork implements PropertyChangedListener } } - if ( ! ($this->entityInsertions || - $this->entityDeletions || - $this->entityUpdates || - $this->collectionUpdates || - $this->collectionDeletions || - $this->orphanRemovals)) { - return; // Nothing to do. - } + $anythingToDo = + $this->entityInsertions || + $this->entityDeletions || + $this->entityUpdates || + $this->collectionUpdates || + $this->collectionDeletions || + $this->orphanRemovals; - if ($this->orphanRemovals) { + if ($anythingToDo && $this->orphanRemovals) { foreach ($this->orphanRemovals as $orphan) { $this->remove($orphan); } @@ -301,57 +300,59 @@ class UnitOfWork implements PropertyChangedListener $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em)); } - // Now we need a commit order to maintain referential integrity - $commitOrder = $this->getCommitOrder(); + if ($anythingToDo) { + // Now we need a commit order to maintain referential integrity + $commitOrder = $this->getCommitOrder(); - $conn = $this->em->getConnection(); - $conn->beginTransaction(); + $conn = $this->em->getConnection(); + $conn->beginTransaction(); - try { - if ($this->entityInsertions) { - foreach ($commitOrder as $class) { - $this->executeInserts($class); + try { + if ($this->entityInsertions) { + foreach ($commitOrder as $class) { + $this->executeInserts($class); + } } - } - if ($this->entityUpdates) { - foreach ($commitOrder as $class) { - $this->executeUpdates($class); + if ($this->entityUpdates) { + foreach ($commitOrder as $class) { + $this->executeUpdates($class); + } } - } - // Extra updates that were requested by persisters. - if ($this->extraUpdates) { - $this->executeExtraUpdates(); - } - - // Collection deletions (deletions of complete collections) - foreach ($this->collectionDeletions as $collectionToDelete) { - $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); - } - // Collection updates (deleteRows, updateRows, insertRows) - foreach ($this->collectionUpdates as $collectionToUpdate) { - $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); - } - - // Entity deletions come last and need to be in reverse commit order - if ($this->entityDeletions) { - for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) { - $this->executeDeletions($commitOrder[$i]); + // Extra updates that were requested by persisters. + if ($this->extraUpdates) { + $this->executeExtraUpdates(); } + + // Collection deletions (deletions of complete collections) + foreach ($this->collectionDeletions as $collectionToDelete) { + $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); + } + // Collection updates (deleteRows, updateRows, insertRows) + foreach ($this->collectionUpdates as $collectionToUpdate) { + $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); + } + + // Entity deletions come last and need to be in reverse commit order + if ($this->entityDeletions) { + for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) { + $this->executeDeletions($commitOrder[$i]); + } + } + + $conn->commit(); + } catch (Exception $e) { + $this->em->close(); + $conn->rollback(); + + throw $e; } - $conn->commit(); - } catch (Exception $e) { - $this->em->close(); - $conn->rollback(); - - throw $e; - } - - // Take new snapshots from visited collections - foreach ($this->visitedCollections as $coll) { - $coll->takeSnapshot(); + // Take new snapshots from visited collections + foreach ($this->visitedCollections as $coll) { + $coll->takeSnapshot(); + } } // Raise postFlush From 1e669132c27650322850d1d535e6e523eb09dd1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesc=20Ros=C3=A0s?= Date: Mon, 23 Jul 2012 01:14:02 +0200 Subject: [PATCH 2/3] No huge if clause --- lib/Doctrine/ORM/UnitOfWork.php | 134 +++++++++++++++++--------------- 1 file changed, 72 insertions(+), 62 deletions(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 6274ec6d4..24f05748d 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -281,85 +281,81 @@ class UnitOfWork implements PropertyChangedListener } } - $anythingToDo = - $this->entityInsertions || - $this->entityDeletions || - $this->entityUpdates || - $this->collectionUpdates || - $this->collectionDeletions || - $this->orphanRemovals; + if ( ! ($this->entityInsertions || + $this->entityDeletions || + $this->entityUpdates || + $this->collectionUpdates || + $this->collectionDeletions || + $this->orphanRemovals)) { + $this->dispatchOnFlushEvent(); + $this->dispatchPostFlushEvent(); - if ($anythingToDo && $this->orphanRemovals) { + return; // Nothing to do. + } + + if ($this->orphanRemovals) { foreach ($this->orphanRemovals as $orphan) { $this->remove($orphan); } } - // Raise onFlush - if ($this->evm->hasListeners(Events::onFlush)) { - $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em)); - } + $this->dispatchOnFlushEvent(); - if ($anythingToDo) { - // Now we need a commit order to maintain referential integrity - $commitOrder = $this->getCommitOrder(); + // Now we need a commit order to maintain referential integrity + $commitOrder = $this->getCommitOrder(); - $conn = $this->em->getConnection(); - $conn->beginTransaction(); + $conn = $this->em->getConnection(); + $conn->beginTransaction(); - try { - if ($this->entityInsertions) { - foreach ($commitOrder as $class) { - $this->executeInserts($class); - } + try { + if ($this->entityInsertions) { + foreach ($commitOrder as $class) { + $this->executeInserts($class); } - - if ($this->entityUpdates) { - foreach ($commitOrder as $class) { - $this->executeUpdates($class); - } - } - - // Extra updates that were requested by persisters. - if ($this->extraUpdates) { - $this->executeExtraUpdates(); - } - - // Collection deletions (deletions of complete collections) - foreach ($this->collectionDeletions as $collectionToDelete) { - $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); - } - // Collection updates (deleteRows, updateRows, insertRows) - foreach ($this->collectionUpdates as $collectionToUpdate) { - $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); - } - - // Entity deletions come last and need to be in reverse commit order - if ($this->entityDeletions) { - for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) { - $this->executeDeletions($commitOrder[$i]); - } - } - - $conn->commit(); - } catch (Exception $e) { - $this->em->close(); - $conn->rollback(); - - throw $e; } - // Take new snapshots from visited collections - foreach ($this->visitedCollections as $coll) { - $coll->takeSnapshot(); + if ($this->entityUpdates) { + foreach ($commitOrder as $class) { + $this->executeUpdates($class); + } } + + // Extra updates that were requested by persisters. + if ($this->extraUpdates) { + $this->executeExtraUpdates(); + } + + // Collection deletions (deletions of complete collections) + foreach ($this->collectionDeletions as $collectionToDelete) { + $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); + } + // Collection updates (deleteRows, updateRows, insertRows) + foreach ($this->collectionUpdates as $collectionToUpdate) { + $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); + } + + // Entity deletions come last and need to be in reverse commit order + if ($this->entityDeletions) { + for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) { + $this->executeDeletions($commitOrder[$i]); + } + } + + $conn->commit(); + } catch (Exception $e) { + $this->em->close(); + $conn->rollback(); + + throw $e; } - // Raise postFlush - if ($this->evm->hasListeners(Events::postFlush)) { - $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->em)); + // Take new snapshots from visited collections + foreach ($this->visitedCollections as $coll) { + $coll->takeSnapshot(); } + $this->dispatchPostFlushEvent(); + // Clear up $this->entityInsertions = $this->entityUpdates = @@ -3154,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)); + } + } } From 512a001e8c8cec368875be1c8a51d672546756a6 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 6 Jan 2013 19:11:52 +0100 Subject: [PATCH 3/3] DDC-2173 - Add Test for new OnFlush or PreFlush behavior and update UPGRADE.md --- UPGRADE.md | 8 +++++ .../Tests/ORM/Functional/FlushEventTest.php | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+) 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/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++; + } +}