Skip to content

Commit 7b93c1f

Browse files
committed
Reword
1 parent 3759d16 commit 7b93c1f

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

doctrine/events.rst

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ Doctrine Entity Listeners
235235

236236
Entity listeners are defined as PHP classes that listen to a single Doctrine
237237
event on a single entity class. For example, suppose that you want to send some
238-
notifications whenever a ``User`` entity is modified in the database. To do so,
239-
define a listener for the ``postUpdate`` Doctrine event::
238+
notifications whenever a ``User`` entity is modified in the database.
239+
240+
First, define a PHP class that handles the ``postUpdate`` Doctrine event::
240241

241242
// src/EventListener/UserChangedNotifier.php
242243
namespace App\EventListener;
@@ -254,9 +255,27 @@ define a listener for the ``postUpdate`` Doctrine event::
254255
}
255256
}
256257

257-
The next step is to enable the Doctrine listener in the Symfony application by
258-
creating a new service for it and :doc:`tagging it </service_container/tags>`
259-
with the ``doctrine.orm.entity_listener`` tag:
258+
Then, add the ``#[AsDoctrineListener]`` attribute to the class to enable it as
259+
a Doctrine entity listener in your application:
260+
261+
.. code-block:: php
262+
263+
// src/EventListener/UserChangedNotifier.php
264+
namespace App\EventListener;
265+
266+
// ...
267+
use App\Entity\User;
268+
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
269+
270+
#[AsDoctrineListener(event: 'postUpdate', method: 'postUpdate', entity: User::class)]
271+
class UserChangedNotifier
272+
{
273+
// ...
274+
}
275+
276+
That's it. Alternatively, if you prefer to not use PHP attributes, you must
277+
configure a service for the entity listener and :doc:`tag it </service_container/tags>`
278+
with the ``doctrine.orm.entity_listener`` tag as follows:
260279

261280
.. configuration-block::
262281

@@ -353,30 +372,6 @@ with the ``doctrine.orm.entity_listener`` tag:
353372
;
354373
};
355374
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-
380375
Doctrine Lifecycle Subscribers
381376
------------------------------
382377

0 commit comments

Comments
 (0)