Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Bundles
  4. DoctrineBundle
  5. Entity Listeners
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia
  • Lazy Entity Listeners

Entity Listeners

Edit this page

Entity Listeners

Entity listeners that are services must be registered with the entity listener resolver. On top of the annotation in the entity class, you have to tag the service with doctrine.orm.entity_listener for it to be automatically added to the resolver. Use the (optional) entity_manager attribute to specify which entity manager it should be registered with.

Full example:

  • Annotations
  • Attributes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// User.php

use Doctrine\ORM\Mapping as ORM;
use App\UserListener;

/**
 * @ORM\Entity
 * @ORM\EntityListeners({UserListener::class})
 */
class User
{
    // ....
}
1
2
3
4
5
6
7
8
9
10
11
12
<?php
// User.php

use Doctrine\ORM\Mapping as ORM;
use App\UserListener;

#[ORM\Entity]
#[ORM\EntityListeners([UserListener::class])]
class User
{
    // ....
}
  • YAML
  • XML
1
2
3
4
5
6
7
services:
    App\UserListener:
        tags:
            # Minimal configuration below
            - { name: doctrine.orm.entity_listener }
            # Or, optionally, you can give the entity manager name as below
            #- { name: doctrine.orm.entity_listener, entity_manager: custom }
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <services>
        <service id="App\UserListener">
            <!-- entity_manager attribute is optional -->
            <tag name="doctrine.orm.entity_listener" entity_manager="custom" />
        </service>
    </services>
</container>

Starting with doctrine/orm 2.5 and Doctrine bundle 1.5.2, instead of registering the entity listener on the entity, you can declare all options from the service definition:

  • YAML
  • XML
1
2
3
4
5
6
7
8
9
10
11
services:
    App\UserListener:
        tags:
            -
                name: doctrine.orm.entity_listener
                event: preUpdate
                entity: App\Entity\User
                # entity_manager attribute is optional
                entity_manager: custom
                # method attribute is optional
                method: validateEmail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <services>
        <service id="App\UserListener">
            <!-- entity_manager attribute is optional -->
            <!-- method attribute is optional -->
            <tag
                name="doctrine.orm.entity_listener" 
                event="preUpdate"
                entity="App\Entity\User"
                entity_manager="custom"
                method="validateEmail"
            />
        </service>
    </services>
</container>

The event attribute is required if the entity listener is not registered on the entity. If you don't specify the method attribute, it falls back on the subscribed event name.

Starting with Doctrine bundle 1.12, if this method does not exist but if your entity listener is invokable, it falls back on the __invoke() method.

See also https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners for more info on entity listeners and the resolver required by Symfony.

Lazy Entity Listeners

You can use the lazy attribute on the tag to make sure the listener services are only instantiated when they are actually used.

  • YAML
  • XML
1
2
3
4
services:
    App\UserListener:
        tags:
            - { name: doctrine.orm.entity_listener, lazy: true }
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <services>
        <service id="App\UserListener">
            <tag name="doctrine.orm.entity_listener" event="preUpdate" entity="App\Entity\User" lazy="true" />            
        </service>
    </services>
</container>
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Become certified from home

Become certified from home

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Markus Lanthaler, a Symfony contributor

Thanks Markus Lanthaler (@lanthaler) for being a Symfony contributor

6 commits • 65 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia