From 565e0e3bd1e798727ffc79e9130d0d3cf308e310 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 3 Jan 2023 08:54:04 +1000 Subject: [PATCH] Adds information on AsEntityListener attribute to the Doctrine event documentation --- doctrine/events.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/doctrine/events.rst b/doctrine/events.rst index 9a2756a4e61..71bb44d6fe0 100644 --- a/doctrine/events.rst +++ b/doctrine/events.rst @@ -353,6 +353,30 @@ with the ``doctrine.orm.entity_listener`` tag: ; }; + +Doctrine Entity Listeners may also be defined using PHP attributes. When using PHP attributes it is not necessary to create a service for the listener. + + .. code-block:: php + + // src/EventListener/UserChangedNotifier.php + namespace App\EventListener; + + use App\Entity\User; + use Doctrine\Persistence\Event\LifecycleEventArgs; + use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; + + #[AsEntityListener(event:'postUpdate', method:'postUpdate', entity:User::class)] + class UserChangedNotifier + { + // the entity listener methods receive two arguments: + // the entity instance and the lifecycle event + public function postUpdate(User $user, LifecycleEventArgs $event): void + { + // ... do something to notify the changes + } + } + + Doctrine Lifecycle Subscribers ------------------------------