1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +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 // Evaluate entity listener
if (isset($xmlRoot->{'entity-listeners'})) { if (isset($xmlRoot->{'entity-listeners'})) {
foreach ($xmlRoot->{'entity-listeners'}->{'entity-listener'} as $listenerElement) { foreach ($xmlRoot->{'entity-listeners'}->{'entity-listener'} as $listenerElement) {
$className = (string) $listenerElement['class']; $listeners = array();
foreach ($listenerElement as $type => $callbackElement) { foreach ($listenerElement as $type => $callbackElement) {
list($prefix, $suffix) = explode('-', $type); list($prefix, $suffix) = explode('-', $type);
$eventName = $prefix . ucfirst($suffix);
$methodName = (string) $callbackElement['method'];
$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 // Evaluate entityListeners
if (isset($element['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 ($entityListener as $eventName => $callbackElement){
foreach ($callbackElement as $methodName){ 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 class CmsAddress
{ {
@ -94,6 +97,11 @@ class CmsAddress
*/ */
public $user; public $user;
/**
* @var array
*/
public $prePersistHandlerCalls = array();
public function getId() { public function getId() {
return $this->id; 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) public static function loadMetadata(\Doctrine\ORM\Mapping\ClassMetadataInfo $metadata)
{ {
$metadata->setPrimaryTable(array( $metadata->setPrimaryTable(array(
'name' => 'company_person', '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 ( $metadata->addNamedNativeQuery(array (
'name' => 'find-all', 'name' => 'find-all',
'query' => 'SELECT id, country, city FROM cms_addresses', '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); $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', '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 ( $metadata->addNamedNativeQuery(array (
'name' => 'find-all', 'name' => 'find-all',

View file

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

View file

@ -14,6 +14,10 @@ Doctrine\Tests\Models\CMS\CmsAddress:
resultSetMapping: mapping-count resultSetMapping: mapping-count
query: SELECT COUNT(*) AS count FROM cms_addresses query: SELECT COUNT(*) AS count FROM cms_addresses
entityListeners:
listener[0]:
prePersist: [prePersistHandler]
sqlResultSetMappings: sqlResultSetMappings:
mapping-find-all: mapping-find-all:
entityResult: entityResult:
@ -55,7 +59,7 @@ Doctrine\Tests\Models\CMS\CmsAddress:
type: string type: string
length: 50 length: 50
oneToOne: oneToOne:
address: user:
targetEntity: CmsUser targetEntity: CmsUser
inversedBy: address inversedBy: address
joinColumn: joinColumn:

View file

@ -6,19 +6,22 @@ Doctrine\Tests\Models\Company\CompanyContract:
fix: CompanyFixContract fix: CompanyFixContract
flexible: CompanyFlexContract flexible: CompanyFlexContract
flexultra: CompanyFlexUltraContract flexultra: CompanyFlexUltraContract
entityListeners: entityListeners:
ContractSubscriber: listener[0]:
preFlush: [preFlushHandler] class : ContractSubscriber
postLoad: [postLoadHandler]
postPersist: [postPersistHandler] preFlush: [preFlushHandler]
prePersist: [prePersistHandler] postLoad: [postLoadHandler]
postUpdate: [postUpdateHandler] postPersist: [postPersistHandler]
preUpdate: [preUpdateHandler] prePersist: [prePersistHandler]
postRemove: [postRemoveHandler] postUpdate: [postUpdateHandler]
preRemove: [preRemoveHandler] preUpdate: [preUpdateHandler]
postRemove: [postRemoveHandler]
preRemove: [preRemoveHandler]
id: id:
id: id:

View file

@ -1,20 +1,25 @@
Doctrine\Tests\Models\Company\CompanyFlexUltraContract: Doctrine\Tests\Models\Company\CompanyFlexUltraContract:
type: entity type: entity
entityListeners: entityListeners:
ContractSubscriber: listener[0]:
preFlush: [preFlushHandler] class : ContractSubscriber
postLoad: [postLoadHandler]
postPersist: [postPersistHandler]
prePersist: [prePersistHandler]
postUpdate: [postUpdateHandler]
preUpdate: [preUpdateHandler]
postRemove: [postRemoveHandler]
preRemove: [preRemoveHandler]
FlexUltraContractSubscriber: preFlush: [preFlushHandler]
postLoad: [postLoadHandler]
postPersist: [postPersistHandler]
prePersist: [prePersistHandler]
postUpdate: [postUpdateHandler]
preUpdate: [preUpdateHandler]
postRemove: [postRemoveHandler]
preRemove: [preRemoveHandler]
listener[1]:
class : FlexUltraContractSubscriber
prePersist: [prePersistHandler1, prePersistHandler2] prePersist: [prePersistHandler1, prePersistHandler2]
fields: fields: