Skip to content

Commit 565e0e3

Browse files
ericfernancejaviereguiluz
authored andcommitted
Adds information on AsEntityListener attribute to the Doctrine event documentation
1 parent cbce2e1 commit 565e0e3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

doctrine/events.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,30 @@ with the ``doctrine.orm.entity_listener`` tag:
353353
;
354354
};
355355
356+
357+
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.
358+
359+
.. code-block:: php
360+
361+
// src/EventListener/UserChangedNotifier.php
362+
namespace App\EventListener;
363+
364+
use App\Entity\User;
365+
use Doctrine\Persistence\Event\LifecycleEventArgs;
366+
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
367+
368+
#[AsEntityListener(event:'postUpdate', method:'postUpdate', entity:User::class)]
369+
class UserChangedNotifier
370+
{
371+
// the entity listener methods receive two arguments:
372+
// the entity instance and the lifecycle event
373+
public function postUpdate(User $user, LifecycleEventArgs $event): void
374+
{
375+
// ... do something to notify the changes
376+
}
377+
}
378+
379+
356380
Doctrine Lifecycle Subscribers
357381
------------------------------
358382

0 commit comments

Comments
 (0)