@@ -119,13 +119,13 @@ do so, define a listener for the ``postPersist`` Doctrine event::
119119 namespace App\EventListener;
120120
121121 use App\Entity\Product;
122- use Doctrine\Persistence \Event\LifecycleEventArgs ;
122+ use Doctrine\ORM \Event\PostPersistEventArgs ;
123123
124124 class SearchIndexer
125125 {
126126 // the listener methods receive an argument which gives you access to
127127 // 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
129129 {
130130 $entity = $args->getObject();
131131
@@ -140,6 +140,11 @@ do so, define a listener for the ``postPersist`` Doctrine event::
140140 }
141141 }
142142
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+
143148Then, add the ``#[AsDoctrineListener] `` attribute to the class to enable it as
144149a Doctrine listener in your application::
145150
@@ -167,12 +172,12 @@ listener in the Symfony application by creating a new service for it and
167172 namespace App\EventListener;
168173
169174 use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
170- use Doctrine\ORM\Event\LifecycleEventArgs ;
175+ use Doctrine\ORM\Event\PostPersistEventArgs ;
171176
172177 #[AsDoctrineListener('postPersist'/*, 500, 'default'*/)]
173178 class SearchIndexer
174179 {
175- public function postPersist(LifecycleEventArgs $event): void
180+ public function postPersist(PostPersistEventArgs $event): void
176181 {
177182 // ...
178183 }
@@ -277,13 +282,13 @@ First, define a PHP class that handles the ``postUpdate`` Doctrine event::
277282 namespace App\EventListener;
278283
279284 use App\Entity\User;
280- use Doctrine\Persistence \Event\LifecycleEventArgs ;
285+ use Doctrine\ORM \Event\PostUpdateEventArgs ;
281286
282287 class UserChangedNotifier
283288 {
284289 // the entity listener methods receive two arguments:
285290 // 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
287292 {
288293 // ... do something to notify the changes
289294 }
@@ -420,7 +425,9 @@ want to log all the database activity. To do so, define a subscriber for the
420425 use App\Entity\Product;
421426 use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
422427 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;
424431
425432 class DatabaseActivitySubscriber implements EventSubscriberInterface
426433 {
@@ -436,27 +443,25 @@ want to log all the database activity. To do so, define a subscriber for the
436443 }
437444
438445 // 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
440447 // 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
442449 {
443- $this->logActivity('persist', $args);
450+ $this->logActivity('persist', $args->getObject() );
444451 }
445452
446- public function postRemove(LifecycleEventArgs $args): void
453+ public function postRemove(PostRemoveEventArgs $args): void
447454 {
448- $this->logActivity('remove', $args);
455+ $this->logActivity('remove', $args->getObject() );
449456 }
450457
451- public function postUpdate(LifecycleEventArgs $args): void
458+ public function postUpdate(PostUpdateEventArgs $args): void
452459 {
453- $this->logActivity('update', $args);
460+ $this->logActivity('update', $args->getObject() );
454461 }
455462
456- private function logActivity(string $action, LifecycleEventArgs $args ): void
463+ private function logActivity(string $action, mixed $entity ): void
457464 {
458- $entity = $args->getObject();
459-
460465 // if this subscriber only applies to certain entity types,
461466 // add some code to check the entity type as early as possible
462467 if (!$entity instanceof Product) {
@@ -467,6 +472,11 @@ want to log all the database activity. To do so, define a subscriber for the
467472 }
468473 }
469474
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+
470480If you're using the :ref: `default services.yaml configuration <service-container-services-load-example >`
471481and DoctrineBundle 2.1 (released May 25, 2020) or newer, this example will already
472482work! Otherwise, :ref: `create a service <service-container-creating-service >` for this
0 commit comments