Skip to content

Commit d122e62

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: Adds information on AsEntityListener attribute to the Doctrine event documentation
2 parents ae473a7 + 31f69fa commit d122e62

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

doctrine/events.rst

Lines changed: 24 additions & 5 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

0 commit comments

Comments
 (0)