Permalink
Browse files

give event to lifecycle callbacks

  • Loading branch information...
1 parent dbd0697 commit c6adcda5675036debad35d2ae6df968b458bd4bc @FabioBatSilva committed with fabio.silva Jul 31, 2012
@@ -2434,10 +2434,10 @@ public function setCustomRepositoryClass($repositoryClassName)
*
* @return void
*/
- public function invokeLifecycleCallbacks($lifecycleEvent, $entity)
+ public function invokeLifecycleCallbacks($lifecycleEvent, $entity, EventArgs $event)
{
foreach ($this->lifecycleCallbacks[$lifecycleEvent] as $callback) {
- $entity->$callback();
+ $entity->$callback($event);
}
}
@@ -502,7 +502,13 @@ public function getEntityChangeSet($entity)
*/
public function computeChangeSet(ClassMetadata $class, $entity)
{
- $oid = spl_object_hash($entity);
+ $oid = spl_object_hash($entity);
+ $hasEntityListeners = isset($class->entityListeners[Events::preFlush]);
+ $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preFlush]);
+
+ if ($hasEntityListeners || $hasLifecycleCallbacks) {
+ $event = new PreFlushEventArgs($entity, $this->em);
+ }
if (isset($this->readOnlyObjects[$oid])) {
return;
@@ -513,13 +519,13 @@ public function computeChangeSet(ClassMetadata $class, $entity)
}
// Fire PreFlush lifecycle callbacks
- if (isset($class->lifecycleCallbacks[Events::preFlush])) {
- $class->invokeLifecycleCallbacks(Events::preFlush, $entity);
+ if ($hasLifecycleCallbacks) {
+ $class->invokeLifecycleCallbacks(Events::preFlush, $entity, $event);
}
// Fire PreFlush entity listeners
- if (isset($class->entityListeners[Events::preFlush])) {
- $class->dispatchEntityListeners(Events::preFlush, $entity, new PreFlushEventArgs($this->em));
+ if ($hasEntityListeners) {
+ $class->dispatchEntityListeners(Events::preFlush, $entity, $event);
}
$actualData = array();
@@ -821,12 +827,12 @@ private function persistNew($class, $entity)
$hasEntityListeners = isset($class->entityListeners[Events::prePersist]);
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::prePersist]);
- if ($hasListeners || $hasEntityListeners) {
+ if ($hasListeners || $hasEntityListeners || $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em);
}
if ($hasLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::prePersist, $entity);
+ $class->invokeLifecycleCallbacks(Events::prePersist, $entity, $event);
}
if ($hasEntityListeners) {
@@ -972,12 +978,12 @@ private function executeInserts($class)
foreach ($entities as $entity) {
- if ($hasListeners || $hasEntityListeners) {
+ if ($hasListeners || $hasEntityListeners | $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em);
}
if ($hasLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::postPersist, $entity);
+ $class->invokeLifecycleCallbacks(Events::postPersist, $entity, $event);
}
if ($hasEntityListeners) {
@@ -1015,16 +1021,16 @@ private function executeUpdates($class)
continue;
}
- if ($hasPreUpdateListeners || $hasPreUpdateEntityListeners) {
+ if ($hasPreUpdateListeners || $hasPreUpdateEntityListeners || $hasPreUpdateLifecycleCallbacks) {
$preEvent = new PreUpdateEventArgs($entity, $this->em, $this->entityChangeSets[$oid]);
}
- if ($hasPostUpdateListeners || $hasPostUpdateEntityListeners) {
+ if ($hasPostUpdateListeners || $hasPostUpdateEntityListeners || $hasPostUpdateLifecycleCallbacks) {
$postEvent = new LifecycleEventArgs($entity, $this->em);
}
if ($hasPreUpdateLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::preUpdate, $entity);
+ $class->invokeLifecycleCallbacks(Events::preUpdate, $entity, $preEvent);
$this->recomputeSingleEntityChangeSet($class, $entity);
}
@@ -1044,7 +1050,7 @@ private function executeUpdates($class)
unset($this->entityUpdates[$oid]);
if ($hasPostUpdateLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::postUpdate, $entity);
+ $class->invokeLifecycleCallbacks(Events::postUpdate, $entity, $postEvent);
}
if ($hasPostUpdateEntityListeners) {
@@ -1094,12 +1100,12 @@ private function executeDeletions($class)
$class->reflFields[$class->identifier[0]]->setValue($entity, null);
}
- if ($hasListeners || $hasEntityListeners) {
+ if ($hasListeners || $hasEntityListeners || $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em);
}
if ($hasLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::postRemove, $entity);
+ $class->invokeLifecycleCallbacks(Events::postRemove, $entity, $event);
}
if ($hasEntityListeners) {
@@ -1751,12 +1757,12 @@ private function doRemove($entity, array &$visited)
$hasEntityListeners = isset($class->entityListeners[Events::preRemove]);
$hasListeners = $this->evm->hasListeners(Events::preRemove);
- if ($hasListeners || $hasEntityListeners) {
+ if ($hasListeners || $hasEntityListeners || $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em);
}
if ($hasLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::preRemove, $entity);
+ $class->invokeLifecycleCallbacks(Events::preRemove, $entity, $event);
}
if ($hasEntityListeners) {
@@ -2767,12 +2773,12 @@ public function createEntity($className, array $data, &$hints = array())
$hasEntityListeners = isset($class->entityListeners[Events::postLoad]);
$hasListeners = $this->evm->hasListeners(Events::postLoad);
- if ($hasListeners || $hasEntityListeners) {
+ if ($hasListeners || $hasEntityListeners || $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em);
}
if ($hasLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::postLoad, $entity);
+ $class->invokeLifecycleCallbacks(Events::postLoad, $entity, $event);
}
if ($hasEntityListeners) {
@@ -10,7 +10,7 @@
/**
* @group DDC-1955
*/
-class EntityListenersDispatcherTest extends \Doctrine\Tests\OrmFunctionalTestCase
+class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
@@ -2,7 +2,7 @@
namespace Doctrine\Tests\ORM\Functional\Ticket;
-use Doctrine\ORM\UnitOfWork;
+use Doctrine\ORM\Event\LifecycleEventArgs;
/**
* @group DDC-1707
@@ -25,9 +25,11 @@ public function setUp()
public function testPostLoadOnChild()
{
- $class = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1707Child');
+ $class = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1707Child');
$entity = new DDC1707Child();
- $class->invokeLifecycleCallbacks(\Doctrine\ORM\Events::postLoad, $entity);
+ $event = new LifecycleEventArgs($entity, $this->_em);
+
+ $class->invokeLifecycleCallbacks(\Doctrine\ORM\Events::postLoad, $entity, $event);
$this->assertTrue($entity->postLoad);
}

0 comments on commit c6adcda

Please sign in to comment.