Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Entity listener gets triggered on only given entity unlike event listeners. Examples below do updates to User entity and insert log records into UserLog entity in entity listener when interacting with User entity in controller.


Entity


use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @ORM\EntityListeners({"Application\BackendBundle\EntityListener\UserListener"})
* @ORM\Table(name="user")
*/
class User
{
// Properties, getters and setters as usual
}

Entity Listener


namespace Application\BackendBundle\EntityListener;

use Application\BackendBundle\Entity\User;
use Application\BackendBundle\Entity\UserLog;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping as ORM;

class UserListener
{
// -- PERSIST --------------------------------------------------------

/** @ORM\PrePersist */
public function prePersistHandler(User $user, LifecycleEventArgs $args)
{
$user->setPrePersist(date('d/m/Y H:i:s'));

$userLog = new UserLog();
$userLog->setUserId($user->getId());
$userLog->setMessage('prePersistHandler at ' . date('d/m/Y H:i:s'));
$em = $args->getEntityManager();
$em->persist($userLog);
$em->flush();
}
/** @ORM\PostPersist */
public function postPersistHandler(User $user, LifecycleEventArgs $args)
{
$userLog = new UserLog();
$userLog->setUserId($user->getId());
$userLog->setMessage('postPersistHandler at ' . date('d/m/Y H:i:s'));
$em = $args->getEntityManager();
$em->persist($userLog);
$em->flush();
}

// -- UPDATE ---------------------------------------------------------

private $log = [];
/** @ORM\PreUpdate */
public function preUpdateHandler(User $user, PreUpdateEventArgs $args)
{
$user->setPreUpdate(date('d/m/Y H:i:s'));

$userLog = new UserLog();
$userLog->setUserId($user->getId());
$userLog->setMessage('preUpdateHandler at ' . date('d/m/Y H:i:s'));
$this->log[] = $userLog;
}
/** @ORM\PostUpdate */
public function postUpdateHandler(User $user, LifecycleEventArgs $args)
{
// This part processes left-overs from preUpdateHandler above
if (! empty($this->log)) {
$em = $args->getEntityManager();

foreach ($this->log as $log) {
$em->persist($log);
}

$this->log = [];
$em->flush();
}

// This part is for postUpdateHandler itself
$userLog = new UserLog();
$userLog->setUserId($user->getId());
$userLog->setMessage('postUpdateHandler at ' . date('d/m/Y H:i:s'));
$em = $args->getEntityManager();
$em->persist($userLog);
$em->flush();
}

// -- REMOVE ---------------------------------------------------------

/** @ORM\PreRemove */
public function preRemoveHandler(User $user, LifecycleEventArgs $args)
{
$userLog = new UserLog();
$userLog->setUserId($user->getId());
$userLog->setMessage('preRemoveHandler at ' . date('d/m/Y H:i:s'));
$em = $args->getEntityManager();
$em->persist($userLog);
$em->flush();
}
/** @ORM\PostRemove */
public function postRemoveHandler(User $user, LifecycleEventArgs $args)
{
$userLog = new UserLog();
$userLog->setUserId($user->getId());
$userLog->setMessage('postRemoveHandler at ' . date('d/m/Y H:i:s'));
$em = $args->getEntityManager();
$em->persist($userLog);
$em->flush();
}

// -- FLUSH ----------------------------------------------------------

/** @ORM\PreFlush */
public function preFlushHandler(User $user, PreFlushEventArgs $args)
{
$user->setPreFlush(date('d/m/Y H:i:s'));
}

// -- LOAD ----------------------------------------------------------

/** @ORM\PostLoad */
public function postLoadHandler(User $user, LifecycleEventArgs $args)
{
$em = $args->getEntityManager();

// Note: This will trigger preUpdateHandler and postUpdateHandler as well
$user->setPostLoad(date('d/m/Y H:i:s'));
$em->persist($user);

$userLog = new UserLog();
$userLog->setUserId($user->getId());
$userLog->setMessage('postLoadHandler at ' . date('d/m/Y H:i:s'));
$em->persist($userLog);

$em->flush();
}
}