diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
index f22db0745..22bdf1608 100644
--- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
+++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
@@ -560,14 +560,30 @@ class XmlDriver extends FileDriver
// Evaluate entity listener
if (isset($xmlRoot->{'entity-listeners'})) {
foreach ($xmlRoot->{'entity-listeners'}->{'entity-listener'} as $listenerElement) {
- $className = (string) $listenerElement['class'];
+ $listeners = array();
foreach ($listenerElement as $type => $callbackElement) {
- list($prefix, $suffix) = explode('-', $type);
- $eventName = $prefix . ucfirst($suffix);
- $methodName = (string) $callbackElement['method'];
+ list($prefix, $suffix) = explode('-', $type);
- $metadata->addEntityListener($eventName, $className, $methodName);
+ $eventName = $prefix . ucfirst($suffix);
+ $methodName = (string) $callbackElement['method'];
+ $listeners[] = array($eventName, $methodName);
+
+ }
+
+ if (isset($listenerElement['class'])) {
+ $className = (string) $listenerElement['class'];
+
+ foreach ($listeners as $item) {
+ $metadata->addEntityListener($item[0], $className, $item[1]);
+ }
+
+ continue;
+ }
+
+ // evaluate as lifecycle callback if the listener class is not given.
+ foreach ($listeners as $item) {
+ $metadata->addLifecycleCallback($item[1], $item[0]);
}
}
}
diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
index e0af987e3..dc9fc3829 100644
--- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
+++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
@@ -575,12 +575,34 @@ class YamlDriver extends FileDriver
// Evaluate entityListeners
if (isset($element['entityListeners'])) {
- foreach ($element['entityListeners'] as $className => $entityListener) {
+ foreach ($element['entityListeners'] as $entityListener) {
+ $listeners = array();
+ $className = null;
+
+ if (isset($entityListener['class'])) {
+ $className = $entityListener['class'];
+
+ unset($entityListener['class']);
+ }
+
foreach ($entityListener as $eventName => $callbackElement){
foreach ($callbackElement as $methodName){
- $metadata->addEntityListener($eventName, $className, $methodName);
+ $listeners[] = array($eventName, $methodName);
}
}
+
+ if (null !== $className) {
+ foreach ($listeners as $item){
+ $metadata->addEntityListener($item[0], $className, $item[1]);
+ }
+
+ continue;
+ }
+
+ // evaluate as lifecycle callback if the listener class is not given.
+ foreach ($listeners as $item){
+ $metadata->addLifecycleCallback($item[1], $item[0]);
+ }
}
}
}
diff --git a/tests/Doctrine/Tests/Models/CMS/CmsAddress.php b/tests/Doctrine/Tests/Models/CMS/CmsAddress.php
index 9833f3dfb..16394fd87 100644
--- a/tests/Doctrine/Tests/Models/CMS/CmsAddress.php
+++ b/tests/Doctrine/Tests/Models/CMS/CmsAddress.php
@@ -59,6 +59,9 @@ namespace Doctrine\Tests\Models\CMS;
* )
* })
*
+ * @EntityListeners(callbacks = {
+ * @LifecycleCallback(\Doctrine\ORM\Events::prePersist, method = "prePersistHandler")
+ * })
*/
class CmsAddress
{
@@ -94,6 +97,11 @@ class CmsAddress
*/
public $user;
+ /**
+ * @var array
+ */
+ public $prePersistHandlerCalls = array();
+
public function getId() {
return $this->id;
}
@@ -121,12 +129,41 @@ class CmsAddress
}
}
+ public function prePersistHandler($event)
+ {
+ $this->prePersistHandlerCalls[] = $event;
+ }
+
public static function loadMetadata(\Doctrine\ORM\Mapping\ClassMetadataInfo $metadata)
{
$metadata->setPrimaryTable(array(
'name' => 'company_person',
));
+ $metadata->mapField(array (
+ 'id' => true,
+ 'fieldName' => 'id',
+ 'type' => 'integer',
+ ));
+
+ $metadata->mapField(array (
+ 'fieldName' => 'zip',
+ 'length' => 50,
+ ));
+
+ $metadata->mapField(array (
+ 'fieldName' => 'city',
+ 'length' => 50,
+ ));
+
+ $metadata->mapOneToOne(array(
+ 'fieldName' => 'user',
+ 'targetEntity' => 'CmsUser',
+ 'joinColumns' => array(array('referencedColumnName' => 'id'))
+ ));
+
+ $metadata->addLifecycleCallback('prePersistHandler', 'prePersist');
+
$metadata->addNamedNativeQuery(array (
'name' => 'find-all',
'query' => 'SELECT id, country, city FROM cms_addresses',
diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
index ba9a0e0e9..dc8030f20 100644
--- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
+++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
@@ -857,6 +857,20 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
$this->assertEmpty(ContractSubscriber::$postPersistCalls);
}
+ /**
+ * @group DDC-1955
+ */
+ public function testEventListenersLifecycleCallback()
+ {
+ $em = $this->_getTestEntityManager();
+ $factory = $this->createClassMetadataFactory($em);
+ $metadata = $factory->getMetadataFor('Doctrine\Tests\Models\CMS\CmsAddress');
+
+ $this->assertArrayHasKey('prePersist', $metadata->lifecycleCallbacks);
+ $this->assertCount(1, $metadata->lifecycleCallbacks['prePersist']);
+ $this->assertEquals('prePersistHandler', $metadata->lifecycleCallbacks['prePersist'][0]);
+ }
+
}
/**
diff --git a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.Models.CMS.CmsAddress.php b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.Models.CMS.CmsAddress.php
index 964065cbd..fba8c9db8 100644
--- a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.Models.CMS.CmsAddress.php
+++ b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.Models.CMS.CmsAddress.php
@@ -6,6 +6,29 @@ $metadata->setPrimaryTable(array(
'name' => 'company_person',
));
+$metadata->mapField(array (
+ 'id' => true,
+ 'fieldName' => 'id',
+ 'type' => 'integer',
+));
+
+$metadata->mapField(array (
+ 'fieldName' => 'zip',
+ 'length' => 50,
+));
+
+$metadata->mapField(array (
+ 'fieldName' => 'city',
+ 'length' => 50,
+));
+
+$metadata->mapOneToOne(array(
+ 'fieldName' => 'user',
+ 'targetEntity' => 'CmsUser',
+ 'joinColumns' => array(array('referencedColumnName' => 'id'))
+));
+
+$metadata->addLifecycleCallback('prePersistHandler', 'prePersist');
$metadata->addNamedNativeQuery(array (
'name' => 'find-all',
diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.CMS.CmsAddress.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.CMS.CmsAddress.dcm.xml
index 0af5facda..2afa95d30 100644
--- a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.CMS.CmsAddress.dcm.xml
+++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.CMS.CmsAddress.dcm.xml
@@ -21,6 +21,12 @@
+
+
+
+
+
+
diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.CMS.CmsAddress.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.CMS.CmsAddress.dcm.yml
index 604acb293..cbff49615 100644
--- a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.CMS.CmsAddress.dcm.yml
+++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.CMS.CmsAddress.dcm.yml
@@ -14,6 +14,10 @@ Doctrine\Tests\Models\CMS\CmsAddress:
resultSetMapping: mapping-count
query: SELECT COUNT(*) AS count FROM cms_addresses
+ entityListeners:
+ listener[0]:
+ prePersist: [prePersistHandler]
+
sqlResultSetMappings:
mapping-find-all:
entityResult:
@@ -55,7 +59,7 @@ Doctrine\Tests\Models\CMS\CmsAddress:
type: string
length: 50
oneToOne:
- address:
+ user:
targetEntity: CmsUser
inversedBy: address
joinColumn:
diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.Company.CompanyContract.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.Company.CompanyContract.dcm.yml
index c4e3f3026..bdad810c7 100644
--- a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.Company.CompanyContract.dcm.yml
+++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.Company.CompanyContract.dcm.yml
@@ -6,19 +6,22 @@ Doctrine\Tests\Models\Company\CompanyContract:
fix: CompanyFixContract
flexible: CompanyFlexContract
flexultra: CompanyFlexUltraContract
+
entityListeners:
- ContractSubscriber:
- preFlush: [preFlushHandler]
- postLoad: [postLoadHandler]
+ listener[0]:
+ class : ContractSubscriber
- postPersist: [postPersistHandler]
- prePersist: [prePersistHandler]
+ preFlush: [preFlushHandler]
+ postLoad: [postLoadHandler]
- postUpdate: [postUpdateHandler]
- preUpdate: [preUpdateHandler]
+ postPersist: [postPersistHandler]
+ prePersist: [prePersistHandler]
- postRemove: [postRemoveHandler]
- preRemove: [preRemoveHandler]
+ postUpdate: [postUpdateHandler]
+ preUpdate: [preUpdateHandler]
+
+ postRemove: [postRemoveHandler]
+ preRemove: [preRemoveHandler]
id:
id:
diff --git a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.yml b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.yml
index 48a42e649..1621a6a84 100644
--- a/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.yml
+++ b/tests/Doctrine/Tests/ORM/Mapping/yaml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.yml
@@ -1,20 +1,25 @@
Doctrine\Tests\Models\Company\CompanyFlexUltraContract:
type: entity
+
entityListeners:
- ContractSubscriber:
- preFlush: [preFlushHandler]
- postLoad: [postLoadHandler]
-
- postPersist: [postPersistHandler]
- prePersist: [prePersistHandler]
-
- postUpdate: [postUpdateHandler]
- preUpdate: [preUpdateHandler]
-
- postRemove: [postRemoveHandler]
- preRemove: [preRemoveHandler]
+ listener[0]:
+ class : ContractSubscriber
- FlexUltraContractSubscriber:
+ preFlush: [preFlushHandler]
+ postLoad: [postLoadHandler]
+
+ postPersist: [postPersistHandler]
+ prePersist: [prePersistHandler]
+
+ postUpdate: [postUpdateHandler]
+ preUpdate: [preUpdateHandler]
+
+ postRemove: [postRemoveHandler]
+ preRemove: [preRemoveHandler]
+
+ listener[1]:
+ class : FlexUltraContractSubscriber
+
prePersist: [prePersistHandler1, prePersistHandler2]
fields: