A Doctrine entity listener template for your Symfony project

Published on 2021-04-26 • Modified on 2021-04-26

This is a Doctrine entity listener empty template for Symfony that you can copy-paste in your projects. I wanted to have it here on my blog so I don't have to search for it in my other projects. I'll update as soon as it needs to.


<?php

// src/Doctrine/Listener/UserListener.php

declare(strict_types=1);

namespace App\Doctrine\Listener;

use App\Entity\User;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping as ORM;

/**
 * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners-class
 *
 * Declaration in the entity:
 *
 * @ORM\EntityListeners({"App\Doctrine\Listener\UserListener"})
 */
class UserListener
{
    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#prepersist
     */
    public function prePersist(User $user, LifecycleEventArgs $event): void
    {
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preremove
     */
    public function preRemove(User $user, LifecycleEventArgs $event): void
    {
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preflush
     */
    public function preFlush(User $user, PreFlushEventArgs $event): void
    {
        $em = $event->getEntityManager();
        $em->refresh($user); // fake stuff
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush
     *
     * @throws \Doctrine\ORM\ORMException
     */
    public function onFlush(OnFlushEventArgs $event): void
    {
        $em = $event->getEntityManager();
        foreach ($em->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
            $em->refresh($entity); // fake stuff
        }
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postflush
     */
    public function postFlush(PostFlushEventArgs $event): void
    {
        // $em = $event->getEntityManager();
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate
     */
    public function preUpdate(User $user, PreUpdateEventArgs $event): void
    {
        // Do something when the username is changed.
        if ($event->hasChangedField('username')) {
            $user->setEmail($user->getUsername()); // fake stuff
        }
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postupdate-postremove-postpersist
     */
    public function postUpdate(User $user, LifecycleEventArgs $event): void
    {
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postupdate-postremove-postpersist
     */
    public function postRemove(User $user, LifecycleEventArgs $event): void
    {
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postupdate-postremove-postpersist
     */
    public function postPersist(User $user, LifecycleEventArgs $event): void
    {
    }

    /**
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postload
     */
    public function postLoad(User $user, LifecycleEventArgs $event): void
    {
    }
}

 More on Stackoverflow   Read the doc  Random snippet