Merge branch 'everzet-PreFlush-event'
This commit is contained in:
commit
70f0136c8a
53
lib/Doctrine/ORM/Event/PreFlushEventArgs.php
Normal file
53
lib/Doctrine/ORM/Event/PreFlushEventArgs.php
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -109,6 +109,13 @@ final class Events
|
|||||||
*/
|
*/
|
||||||
const loadClassMetadata = 'loadClassMetadata';
|
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,
|
* The onFlush event occurs when the EntityManager#flush() operation is invoked,
|
||||||
* after any changes to managed entities have been determined but before any
|
* after any changes to managed entities have been determined but before any
|
||||||
|
@ -446,6 +446,10 @@ class AnnotationDriver implements Driver
|
|||||||
if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
|
if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
|
||||||
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($annotations['Doctrine\ORM\Mapping\PreFlush'])) {
|
||||||
|
$metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preFlush);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -387,3 +387,9 @@ final class PostRemove implements Annotation {}
|
|||||||
* @Target("METHOD")
|
* @Target("METHOD")
|
||||||
*/
|
*/
|
||||||
final class PostLoad implements Annotation {}
|
final class PostLoad implements Annotation {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @Target("METHOD")
|
||||||
|
*/
|
||||||
|
final class PreFlush implements Annotation {}
|
||||||
|
@ -259,6 +259,11 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
*/
|
*/
|
||||||
public function commit($entity = null)
|
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.
|
// Compute changes done since last commit.
|
||||||
if ($entity === null) {
|
if ($entity === null) {
|
||||||
$this->computeChangeSets();
|
$this->computeChangeSets();
|
||||||
@ -485,6 +490,11 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
$class = $this->em->getClassMetadata(get_class($entity));
|
$class = $this->em->getClassMetadata(get_class($entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fire PreFlush lifecycle callbacks
|
||||||
|
if (isset($class->lifecycleCallbacks[Events::preFlush])) {
|
||||||
|
$class->invokeLifecycleCallbacks(Events::preFlush, $entity);
|
||||||
|
}
|
||||||
|
|
||||||
$actualData = array();
|
$actualData = array();
|
||||||
|
|
||||||
foreach ($class->reflFields as $name => $refProp) {
|
foreach ($class->reflFields as $name => $refProp) {
|
||||||
|
@ -43,6 +43,29 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$this->assertEquals('changed from preUpdate callback!', $result[0]->value);
|
$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()
|
public function testChangesDontGetLost()
|
||||||
{
|
{
|
||||||
$user = new LifecycleCallbackTestUser;
|
$user = new LifecycleCallbackTestUser;
|
||||||
@ -190,6 +213,8 @@ class LifecycleCallbackTestEntity
|
|||||||
public $postPersistCallbackInvoked = false;
|
public $postPersistCallbackInvoked = false;
|
||||||
public $postLoadCallbackInvoked = false;
|
public $postLoadCallbackInvoked = false;
|
||||||
|
|
||||||
|
public $preFlushCallbackInvoked = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Id @Column(type="integer")
|
* @Id @Column(type="integer")
|
||||||
* @GeneratedValue(strategy="AUTO")
|
* @GeneratedValue(strategy="AUTO")
|
||||||
@ -233,6 +258,11 @@ class LifecycleCallbackTestEntity
|
|||||||
public function doStuffOnPreUpdate() {
|
public function doStuffOnPreUpdate() {
|
||||||
$this->value = 'changed from preUpdate callback!';
|
$this->value = 'changed from preUpdate callback!';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @PreFlush */
|
||||||
|
public function doStuffOnPreFlush() {
|
||||||
|
$this->preFlushCallbackInvoked = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user