@@ -119,13 +119,13 @@ do so, define a listener for the ``postPersist`` Doctrine event::
119
119
namespace App\EventListener;
120
120
121
121
use App\Entity\Product;
122
- use Doctrine\Persistence \Event\LifecycleEventArgs ;
122
+ use Doctrine\ORM \Event\PostPersistEventArgs ;
123
123
124
124
class SearchIndexer
125
125
{
126
126
// the listener methods receive an argument which gives you access to
127
127
// both the entity object of the event and the entity manager itself
128
- public function postPersist(LifecycleEventArgs $args): void
128
+ public function postPersist(PostPersistEventArgs $args): void
129
129
{
130
130
$entity = $args->getObject();
131
131
@@ -140,6 +140,11 @@ do so, define a listener for the ``postPersist`` Doctrine event::
140
140
}
141
141
}
142
142
143
+ .. note ::
144
+
145
+ In previous Doctrine versions, instead of ``PostPersistEventArgs ``, you had
146
+ to use ``LifecycleEventArgs ``, which was deprecated in Doctrine ORM 2.14.
147
+
143
148
Then, add the ``#[AsDoctrineListener] `` attribute to the class to enable it as
144
149
a Doctrine listener in your application::
145
150
@@ -167,12 +172,12 @@ listener in the Symfony application by creating a new service for it and
167
172
namespace App\EventListener;
168
173
169
174
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
170
- use Doctrine\ORM\Event\LifecycleEventArgs ;
175
+ use Doctrine\ORM\Event\PostPersistEventArgs ;
171
176
172
177
#[AsDoctrineListener('postPersist'/*, 500, 'default'*/)]
173
178
class SearchIndexer
174
179
{
175
- public function postPersist(LifecycleEventArgs $event): void
180
+ public function postPersist(PostPersistEventArgs $event): void
176
181
{
177
182
// ...
178
183
}
@@ -277,13 +282,13 @@ First, define a PHP class that handles the ``postUpdate`` Doctrine event::
277
282
namespace App\EventListener;
278
283
279
284
use App\Entity\User;
280
- use Doctrine\Persistence \Event\LifecycleEventArgs ;
285
+ use Doctrine\ORM \Event\PostUpdateEventArgs ;
281
286
282
287
class UserChangedNotifier
283
288
{
284
289
// the entity listener methods receive two arguments:
285
290
// the entity instance and the lifecycle event
286
- public function postUpdate(User $user, LifecycleEventArgs $event): void
291
+ public function postUpdate(User $user, PostUpdateEventArgs $event): void
287
292
{
288
293
// ... do something to notify the changes
289
294
}
@@ -420,7 +425,9 @@ want to log all the database activity. To do so, define a subscriber for the
420
425
use App\Entity\Product;
421
426
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
422
427
use Doctrine\ORM\Events;
423
- use Doctrine\Persistence\Event\LifecycleEventArgs;
428
+ use Doctrine\ORM\Event\PostPersistEventArgs;
429
+ use Doctrine\ORM\Event\PostRemoveEventArgs;
430
+ use Doctrine\ORM\Event\PostUpdateEventArgs;
424
431
425
432
class DatabaseActivitySubscriber implements EventSubscriberInterface
426
433
{
@@ -436,27 +443,25 @@ want to log all the database activity. To do so, define a subscriber for the
436
443
}
437
444
438
445
// callback methods must be called exactly like the events they listen to;
439
- // they receive an argument of type LifecycleEventArgs , which gives you access
446
+ // they receive an argument of type Post*EventArgs , which gives you access
440
447
// to both the entity object of the event and the entity manager itself
441
- public function postPersist(LifecycleEventArgs $args): void
448
+ public function postPersist(PostPersistEventArgs $args): void
442
449
{
443
- $this->logActivity('persist', $args);
450
+ $this->logActivity('persist', $args->getObject() );
444
451
}
445
452
446
- public function postRemove(LifecycleEventArgs $args): void
453
+ public function postRemove(PostRemoveEventArgs $args): void
447
454
{
448
- $this->logActivity('remove', $args);
455
+ $this->logActivity('remove', $args->getObject() );
449
456
}
450
457
451
- public function postUpdate(LifecycleEventArgs $args): void
458
+ public function postUpdate(PostUpdateEventArgs $args): void
452
459
{
453
- $this->logActivity('update', $args);
460
+ $this->logActivity('update', $args->getObject() );
454
461
}
455
462
456
- private function logActivity(string $action, LifecycleEventArgs $args ): void
463
+ private function logActivity(string $action, mixed $entity ): void
457
464
{
458
- $entity = $args->getObject();
459
-
460
465
// if this subscriber only applies to certain entity types,
461
466
// add some code to check the entity type as early as possible
462
467
if (!$entity instanceof Product) {
@@ -467,6 +472,11 @@ want to log all the database activity. To do so, define a subscriber for the
467
472
}
468
473
}
469
474
475
+ .. note ::
476
+
477
+ In previous Doctrine versions, instead of ``Post*EventArgs `` classes, you had
478
+ to use ``LifecycleEventArgs ``, which was deprecated in Doctrine ORM 2.14.
479
+
470
480
If you're using the :ref: `default services.yaml configuration <service-container-services-load-example >`
471
481
and DoctrineBundle 2.1 (released May 25, 2020) or newer, this example will already
472
482
work! Otherwise, :ref: `create a service <service-container-creating-service >` for this
0 commit comments