Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

postFlush event implementation #168

Merged
merged 3 commits into from

3 participants

@dfreudenberger

No description provided.

Daniel Freud... added some commits
Daniel Freudenberger added the postFlush event 46a3fec
Daniel Freudenberger fixed typo in docblock 5e28273
@jwage
Owner

:+1:

lib/Doctrine/ORM/UnitOfWork.php
@@ -329,6 +329,11 @@ class UnitOfWork implements PropertyChangedListener
throw $e;
}
+ // Raise postFlush
@beberlei Owner

Can you move this behind the snapshot taking?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@beberlei beberlei merged commit 9b8d2d5 into doctrine:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 22, 2011
  1. added the postFlush event

    Daniel Freudenberger authored
  2. fixed typo in docblock

    Daniel Freudenberger authored
  3. dispatch the event after the snapshot was taken

    Daniel Freudenberger authored
This page is out of date. Refresh to see the latest.
View
57 lib/Doctrine/ORM/Event/PostFlushEventArgs.php
@@ -0,0 +1,57 @@
+<?php
+/*
+ * $Id$
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+*/
+
+namespace Doctrine\ORM\Event;
+use Doctrine\ORM\EntityManager;
+use Doctrine\Common\EventArgs;
+
+/**
+ * Provides event arguments for the postFlush event.
+ *
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @link www.doctrine-project.com
+ * @since 2.0
+ * @version $Revision$
+ * @author Daniel Freudenberger <df@rebuy.de>
+ */
+class PostFlushEventArgs extends EventArgs
+{
+ /**
+ * @var EntityManager
+ */
+ private $em;
+
+ /**
+ * @param EntityManager $em
+ */
+ public function __construct(EntityManager $em)
+ {
+ $this->em = $em;
+ }
+
+ /**
+ * @return EntityManager
+ */
+ public function getEntityManager()
+ {
+ return $this->em;
+ }
+}
View
11 lib/Doctrine/ORM/Events.php
@@ -121,6 +121,17 @@ private function __construct() {}
const onFlush = 'onFlush';
/**
+ * The postFlush event occurs when the EntityManager#flush() operation is invoked and
+ * after all actual database operations are executed successfully. The event is only raised if there is
+ * actually something to do for the underlying UnitOfWork. If nothing needs to be done,
+ * the postFlush event is not raised. The event won't be raised if an error occurs during the
+ * flush operation.
+ *
+ * @var string
+ */
+ const postFlush = 'postFlush';
+
+ /**
* The onClear event occurs when the EntityManager#clear() operation is invoked,
* after all references to entities have been removed from the unit of work.
*
View
7 lib/Doctrine/ORM/UnitOfWork.php
@@ -328,12 +328,17 @@ public function commit()
$conn->rollback();
throw $e;
}
-
+
// Take new snapshots from visited collections
foreach ($this->visitedCollections as $coll) {
$coll->takeSnapshot();
}
+ // Raise postFlush
+ if ($this->evm->hasListeners(Events::postFlush)) {
+ $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->em));
+ }
+
// Clear up
$this->entityInsertions =
$this->entityUpdates =
View
95 tests/Doctrine/Tests/ORM/Functional/PostFlushEventTest.php
@@ -0,0 +1,95 @@
+<?php
+namespace Doctrine\Tests\ORM\Functional;
+use Doctrine\Tests\Models\CMS\CmsUser;
+use Doctrine\ORM\Event\PostFlushEventArgs;
+use Doctrine\ORM\Events;
+require_once __DIR__ . '/../../TestInit.php';
+
+/**
+ * PostFlushEventTest
+ *
+ * @author Daniel Freudenberger <df@rebuy.de>
+ */
+class PostFlushEventTest extends \Doctrine\Tests\OrmFunctionalTestCase
+{
+ /**
+ * @var PostFlushListener
+ */
+ private $listener;
+
+ protected function setUp()
+ {
+ parent::setUp();
+ $this->useModelSet('cms');
+ $this->listener = new PostFlushListener();
+ $evm = $this->_em->getEventManager();
+ $evm->addEventListener(Events::postFlush, $this->listener);
+ }
+
+ public function testListenerShouldBeNotified()
+ {
+ $this->_em->persist($this->createNewValidUser());
+ $this->_em->flush();
+ $this->assertTrue($this->listener->wasNotified);
+ }
+
+ public function testListenerShouldNotBeNotifiedWhenFlushThrowsException()
+ {
+ $user = new CmsUser();
+ $user->username = 'dfreudenberger';
+ $this->_em->persist($user);
+ $exceptionRaised = false;
+
+ try {
+ $this->_em->flush();
+ } catch (\Exception $ex) {
+ $exceptionRaised = true;
+ }
+
+ $this->assertTrue($exceptionRaised);
+ $this->assertFalse($this->listener->wasNotified);
+ }
+
+ public function testListenerShouldReceiveEntityManagerThroughArgs()
+ {
+ $this->_em->persist($this->createNewValidUser());
+ $this->_em->flush();
+ $receivedEm = $this->listener->receivedArgs->getEntityManager();
+ $this->assertSame($this->_em, $receivedEm);
+ }
+
+ /**
+ * @return CmsUser
+ */
+ private function createNewValidUser()
+ {
+ $user = new CmsUser();
+ $user->username = 'dfreudenberger';
+ $user->name = 'Daniel Freudenberger';
+ return $user;
+ }
+}
+
+class PostFlushListener
+{
+ /**
+ * @var bool
+ */
+ public $wasNotified = false;
+
+ /**
+ * @var PostFlushEventArgs
+ */
+ public $receivedArgs;
+
+ /**
+ * @param PostFlushEventArgs $args
+ */
+ public function postFlush(PostFlushEventArgs $args)
+ {
+ $this->wasNotified = true;
+ $this->receivedArgs = $args;
+ }
+}
+
+
Something went wrong with that request. Please try again.