1
0
mirror of synced 2025-01-18 06:21:40 +03:00

evaluate as lifecycle callback if the listener class is not given.

This commit is contained in:
Fabio B. Silva 2012-08-09 22:05:22 -03:00 committed by fabio.silva
parent fd6f592430
commit 256cecbefa
9 changed files with 160 additions and 30 deletions

View File

@ -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]);
}
}
}

View File

@ -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]);
}
}
}
}

View File

@ -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',

View File

@ -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]);
}
}
/**

View File

@ -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',

View File

@ -21,6 +21,12 @@
</named-native-query>
</named-native-queries>
<entity-listeners>
<entity-listener>
<pre-persist method="prePersistHandler"/>
</entity-listener>
</entity-listeners>
<sql-result-set-mappings>
<sql-result-set-mapping name="mapping-find-all">
<entity-result entity-class="CmsAddress">

View File

@ -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:

View File

@ -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:

View File

@ -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: