Un squelette d'écouteur d'entité Doctrine pour votre projet Symfony

Publié le 26/04/2021 • Mis à jour le 26/04/2021


English language detected! 🇬🇧

  We noticed that your browser is using English. Do you want to read this post in this language?

Read the english version 🇬🇧 Close

Vous trouverez ici un squelette d'écouteur d'entité Doctrine vide pour Symfony afin de pouvoir le copier-coller facilement dans vos propres projets. Je voulais l'avoir à ma disposition ici afin de ne pas avoir à le chercher dans le code source de mes différents projets. Je le mettrai à jour régulièrement dès que nécessaire.


<?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
    {
    }
}

 Plus sur Stackoverflow   Lire la doc  Snippet aléatoire