Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preFlush event and lifecycle callback #169

Merged
merged 4 commits into from
Nov 13, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lib/Doctrine/ORM/Event/PreFlushEventArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;

/**
* Provides event arguments for the preFlush event.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.0
* @version $Revision$
* @author Roman Borschel <roman@code-factory.de>
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class PreFlushEventArgs extends \Doctrine\Common\EventArgs
{
/**
* @var EntityManager
*/
private $_em;

public function __construct($em)
{
$this->_em = $em;
}

/**
* @return EntityManager
*/
public function getEntityManager()
{
return $this->_em;
}
}
7 changes: 7 additions & 0 deletions lib/Doctrine/ORM/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ private function __construct() {}
*/
const loadClassMetadata = 'loadClassMetadata';

/**
* The preFlush event occurs when the EntityManager#flush() operation is invoked,
* but before any changes to managed entites have been calculated. This event is
* always raised right after EntityManager#flush() call.
*/
const preFlush = 'preFlush';

/**
* The onFlush event occurs when the EntityManager#flush() operation is invoked,
* after any changes to managed entities have been determined but before any
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
}

if (isset($annotations['Doctrine\ORM\Mapping\PreFlush'])) {
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preFlush);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,9 @@ final class PostRemove implements Annotation {}
* @Target("METHOD")
*/
final class PostLoad implements Annotation {}

/**
* @Annotation
* @Target("METHOD")
*/
final class PreFlush implements Annotation {}
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ public function __construct(EntityManager $em)
*/
public function commit($entity = null)
{
// Raise preFlush
if ($this->evm->hasListeners(Events::preFlush)) {
$this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->em));
}

// Compute changes done since last commit.
if ($entity === null) {
$this->computeChangeSets();
Expand Down Expand Up @@ -481,6 +486,11 @@ public function computeChangeSet(ClassMetadata $class, $entity)
return;
}

// Fire PreFlush lifecycle callbacks
if (isset($class->lifecycleCallbacks[Events::preFlush])) {
$class->invokeLifecycleCallbacks(Events::preFlush, $entity);
}

$actualData = array();
foreach ($class->reflFields as $name => $refProp) {
$value = $refProp->getValue($entity);
Expand Down
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ public function testPreSavePostSaveCallbacksAreInvoked()
$this->assertEquals('changed from preUpdate callback!', $result[0]->value);
}

public function testPreFlushCallbacksAreInvoked()
{
$entity = new LifecycleCallbackTestEntity;
$entity->value = 'hello';
$this->_em->persist($entity);

$this->_em->flush();

$this->assertTrue($entity->prePersistCallbackInvoked);
$this->assertTrue($entity->preFlushCallbackInvoked);

$entity->preFlushCallbackInvoked = false;
$this->_em->flush();

$this->assertTrue($entity->preFlushCallbackInvoked);

$entity->value = 'bye';
$entity->preFlushCallbackInvoked = false;
$this->_em->flush();

$this->assertTrue($entity->preFlushCallbackInvoked);
}

public function testChangesDontGetLost()
{
$user = new LifecycleCallbackTestUser;
Expand Down Expand Up @@ -190,6 +213,8 @@ class LifecycleCallbackTestEntity
public $postPersistCallbackInvoked = false;
public $postLoadCallbackInvoked = false;

public $preFlushCallbackInvoked = false;

/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
Expand Down Expand Up @@ -233,6 +258,11 @@ public function doStuffOnPostLoad() {
public function doStuffOnPreUpdate() {
$this->value = 'changed from preUpdate callback!';
}

/** @PreFlush */
public function doStuffOnPreFlush() {
$this->preFlushCallbackInvoked = true;
}
}

/**
Expand Down