Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support @LifecycleCallback in @EntityListeners

  • Loading branch information...
commit fd6f59243069e5d5b5073d5de0b7949fc5455286 1 parent 7021f00
@FabioBatSilva authored fabio.silva committed
View
8 lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
@@ -435,6 +435,14 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
}
}
}
+
+ // 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
1  lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php
@@ -65,3 +65,4 @@
require_once __DIR__.'/../AttributeOverride.php';
require_once __DIR__.'/../AttributeOverrides.php';
require_once __DIR__.'/../EntityListeners.php';
+require_once __DIR__.'/../LifecycleCallback.php';
View
9 lib/Doctrine/ORM/Mapping/EntityListeners.php
@@ -37,5 +37,12 @@
*
* @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
41 lib/Doctrine/ORM/Mapping/LifecycleCallback.php
@@ -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
11 tests/Doctrine/Tests/Models/Company/CompanyPerson.php
@@ -15,6 +15,9 @@
* "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 @@ public function setSpouse(CompanyPerson $spouse) {
}
}
+ public function prePersistHandler($event)
+ {
+ $this->prePersistHandlerCalls[] = $event;
+ }
+
+
public static function loadMetadata(\Doctrine\ORM\Mapping\ClassMetadataInfo $metadata)
{
View
17 tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php
@@ -254,6 +254,23 @@ public function testLifecycleCallbackEventArgs()
$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 */
Please sign in to comment.
Something went wrong with that request. Please try again.