1
0
mirror of synced 2025-02-20 06:03:15 +03:00

support @LifecycleCallback in @EntityListeners

This commit is contained in:
Fabio B. Silva 2012-08-07 22:34:32 -03:00 committed by fabio.silva
parent 7021f002f2
commit fd6f592430
6 changed files with 86 additions and 1 deletions

View File

@ -435,6 +435,14 @@ class AnnotationDriver extends AbstractAnnotationDriver
}
}
}
// evaluate as lifecycle callback if the listener class is not given.
if(empty($entityListenersAnnot->value)) {
/* @var $method \Doctrine\ORM\Mapping\LifecycleCallback */
foreach ($entityListenersAnnot->callbacks as $callback) {
$metadata->addLifecycleCallback($callback->method, $callback->event);
}
}
}
// Evaluate @HasLifecycleCallbacks annotation

View File

@ -65,3 +65,4 @@ require_once __DIR__.'/../AssociationOverrides.php';
require_once __DIR__.'/../AttributeOverride.php';
require_once __DIR__.'/../AttributeOverrides.php';
require_once __DIR__.'/../EntityListeners.php';
require_once __DIR__.'/../LifecycleCallback.php';

View File

@ -37,5 +37,12 @@ final class EntityListeners implements Annotation
*
* @var array<string>
*/
public $value;
public $value = array();
/**
* Specifies the entity the entity lifecycle callbacks.
*
* @var array<\Doctrine\ORM\Mapping\LifecycleCallback>
*/
public $callbacks = array();
}

View File

@ -0,0 +1,41 @@
<?php
/*
* 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 MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Mapping;
/**
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
* @since 2.4
*
* @Annotation
* @Target("ANNOTATION")
*/
final class LifecycleCallback implements Annotation
{
/**
* @var string
*/
public $event;
/**
* @var string
*/
public $method;
}

View File

@ -15,6 +15,9 @@ namespace Doctrine\Tests\Models\Company;
* "manager" = "CompanyManager",
* "employee" = "CompanyEmployee"
* })
* @EntityListeners(callbacks = {
* @LifecycleCallback(\Doctrine\ORM\Events::prePersist, method = "prePersistHandler")
* })
*
* @NamedNativeQueries({
* @NamedNativeQuery(
@ -79,6 +82,8 @@ class CompanyPerson
*/
private $friends;
public $prePersistHandlerCalls = array();
public function __construct() {
$this->friends = new \Doctrine\Common\Collections\ArrayCollection;
}
@ -117,6 +122,12 @@ class CompanyPerson
}
}
public function prePersistHandler($event)
{
$this->prePersistHandlerCalls[] = $event;
}
public static function loadMetadata(\Doctrine\ORM\Mapping\ClassMetadataInfo $metadata)
{

View File

@ -254,6 +254,23 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
$e->calls['postRemoveHandler']
);
}
/**
* @group DDC-1955
*/
public function testEventListenersLifecycleCallback()
{
$e = new \Doctrine\Tests\Models\Company\CompanyPerson;
$e->setName('Fabio B. Silva');
$this->_em->persist($e);
$this->_em->flush();
$this->assertCount(1, $e->prePersistHandlerCalls);
$this->assertInstanceOf(
'Doctrine\ORM\Event\LifecycleEventArgs',
$e->prePersistHandlerCalls[0]
);
}
}
/** @Entity @HasLifecycleCallbacks */