Skip to content

Event Listeners

Edit this page

In opposite to Entity Listeners, Event listeners are services that listen for all entities in your application.

See https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#implementing-event-listeners for more info on event listeners.

To register a service to act as an event listener you have to tag it with the doctrine.event_listener tag:

Starting with Doctrine bundle 2.8, you can use the AsDoctrineListener attribute to tag the service.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// src/App/EventListener/SearchIndexer.php
namespace App\EventListener;

use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\LifecycleEventArgs;

#[AsDoctrineListener('postPersist'/*, 500, 'default'*/)]
class SearchIndexer
{
    public function postPersist(LifecycleEventArgs $event): void
    {
        // ...
    }
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version