From 1f50dee8a84e8493f58fbb30052709e082455cce Mon Sep 17 00:00:00 2001 From: Michael Ridgway Date: Mon, 21 Mar 2011 23:17:08 -0400 Subject: [PATCH 1/3] DDC-696: Added onClear event --- lib/Doctrine/ORM/Event/OnClearEventArgs.php | 56 +++++++++++++++++++ lib/Doctrine/ORM/Events.php | 8 +++ lib/Doctrine/ORM/UnitOfWork.php | 4 ++ .../Tests/ORM/Functional/AllTests.php | 1 + .../Tests/ORM/Functional/ClearEventTest.php | 44 +++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 lib/Doctrine/ORM/Event/OnClearEventArgs.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php diff --git a/lib/Doctrine/ORM/Event/OnClearEventArgs.php b/lib/Doctrine/ORM/Event/OnClearEventArgs.php new file mode 100644 index 000000000..0fe4a4fdd --- /dev/null +++ b/lib/Doctrine/ORM/Event/OnClearEventArgs.php @@ -0,0 +1,56 @@ +. +*/ + +namespace Doctrine\ORM\Event; + +/** + * Provides event arguments for the onClear event. + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.com + * @since 2.0 + * @version $Revision$ + * @author Roman Borschel + * @author Benjamin Eberlei + */ +class OnClearEventArgs extends \Doctrine\Common\EventArgs +{ + /** + * @var \Doctrine\ORM\EntityManager + */ + private $em; + + /** + * @param \Doctrine\ORM\EntityManager $em + */ + public function __construct($em) + { + $this->em = $em; + } + + /** + * @return \Doctrine\ORM\EntityManager + */ + public function getEntityManager() + { + return $this->em; + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Events.php b/lib/Doctrine/ORM/Events.php index e25de658a..8344b07c1 100644 --- a/lib/Doctrine/ORM/Events.php +++ b/lib/Doctrine/ORM/Events.php @@ -119,4 +119,12 @@ final class Events * @var string */ const onFlush = 'onFlush'; + + /** + * The onClear event occurs when the EntityManager#clear() operation is invoked, + * after all references to entities have been removed from the unit of work. + * + * @var string + */ + const onClear = 'onClear'; } \ No newline at end of file diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index db29d9c64..79c946f87 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -1790,6 +1790,10 @@ class UnitOfWork implements PropertyChangedListener if ($this->commitOrderCalculator !== null) { $this->commitOrderCalculator->clear(); } + + if ($this->evm->hasListeners(Events::onClear)) { + $this->evm->dispatchEvent(Events::onClear, new Event\OnClearEventArgs($this->em)); + } } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/AllTests.php b/tests/Doctrine/Tests/ORM/Functional/AllTests.php index 077ee4a7c..cac410281 100644 --- a/tests/Doctrine/Tests/ORM/Functional/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Functional/AllTests.php @@ -58,6 +58,7 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Functional\DatabaseDriverTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\PostgreSQLIdentityStrategyTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\ExtraLazyCollectionTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Functional\ClearEventTest'); $suite->addTest(Locking\AllTests::suite()); $suite->addTest(Ticket\AllTests::suite()); diff --git a/tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php b/tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php new file mode 100644 index 000000000..e95a4cd61 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php @@ -0,0 +1,44 @@ +_em->getEventManager()->addEventListener(Events::onClear, $listener); + + $this->_em->clear(); + + $this->assertTrue($listener->called); + } +} + +class OnClearListener +{ + public $called = false; + + public function onClear(OnClearEventArgs $args) + { + $this->called = true; + } +} + + From 17cbb349523295a1c0879809a0a266b9075b7f56 Mon Sep 17 00:00:00 2001 From: Michael Ridgway Date: Mon, 21 Mar 2011 23:30:10 -0400 Subject: [PATCH 2/3] Clean up of test case --- tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php b/tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php index e95a4cd61..e86edb5b7 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ClearEventTest.php @@ -2,8 +2,6 @@ namespace Doctrine\Tests\ORM\Functional; -use Doctrine\Tests\Models\CMS\CmsUser; -use Doctrine\Tests\Models\CMS\CmsPhonenumber; use Doctrine\ORM\Event\OnClearEventArgs; use Doctrine\ORM\Events; @@ -12,7 +10,7 @@ require_once __DIR__ . '/../../TestInit.php'; /** * ClearEventTest * - * @author Michael Ridgway + * @author Michael Ridgway */ class ClearEventTest extends \Doctrine\Tests\OrmFunctionalTestCase { @@ -39,6 +37,4 @@ class OnClearListener { $this->called = true; } -} - - +} \ No newline at end of file From 706cc838e51a5f3583600ec58ed80229b17d3c21 Mon Sep 17 00:00:00 2001 From: Michael Ridgway Date: Tue, 22 Mar 2011 08:54:33 -0400 Subject: [PATCH 3/3] Removed svn variable --- lib/Doctrine/ORM/Event/OnClearEventArgs.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Doctrine/ORM/Event/OnClearEventArgs.php b/lib/Doctrine/ORM/Event/OnClearEventArgs.php index 0fe4a4fdd..ad89fbc90 100644 --- a/lib/Doctrine/ORM/Event/OnClearEventArgs.php +++ b/lib/Doctrine/ORM/Event/OnClearEventArgs.php @@ -1,7 +1,5 @@