sajad torkamani

Register entity listener

Map the entity listener class using annotations.

<?php
namespace MyProject\Entity;
use App\EventListener\UserListener;

#[Entity]
#[EntityListeners([UserListener::class])]
class User
{
    // ....
}

Define entity listener

<?php
use Doctrine\ORM\Event\PreUpdateEventArgs;

class UserListener
{
    public function preUpdate(User $user, PreUpdateEventArgs $event)
    {
        // Do something on pre update.
    }
}

Other notes

  • An entity listener is invoked just for an entity, as opposed to Doctrine events which are invoked for all entities.

Sources

Tagged: Doctrine

Leave a comment

Your email address will not be published. Required fields are marked *