Skip to content

Commit 3759d16

Browse files
committed
minor #17644 [Doctrine] Adds information on AsEntityListener attribute to the Doctrine event … (Eric)
This PR was submitted for the 6.2 branch but it was merged into the 6.0 branch instead. Discussion ---------- [Doctrine] Adds information on AsEntityListener attribute to the Doctrine event … Doctrine Entity Listeners can also now be defined using the AsEntityListener attribute. This does not require the additional step of creating a service. Documentation about this is missing from the documentation here (https://symfony.com/doc/current/doctrine/events.html#doctrine-entity-listeners). This PR adds this to the Doctrine Entity Listeners documentation. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `6.x` for features of unreleased versions). --> Commits ------- 565e0e3 Adds information on AsEntityListener attribute to the Doctrine event documentation
2 parents cbce2e1 + 565e0e3 commit 3759d16

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)