Un squelette d'écouteur d'entité Doctrine pour votre projet Symfony
Publié le 26/04/2021 • Actualisé le 26/04/2021
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\OnFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\PostLoadEventArgs;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostRemoveEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Event\PreRemoveEventArgs;
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"})
*/
final class UserListener
{
/**
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#prepersist
*/
public function prePersist(User $user, PrePersistEventArgs $event): void
{
}
/**
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preremove
*/
public function preRemove(User $user, PreRemoveEventArgs $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->getObjectManager();
// $em->refresh($user); // fake stuff
}
/**
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush
*/
public function onFlush(OnFlushEventArgs $event): void
{
$em = $event->getObjectManager();
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->getUserIdentifier()); // fake stuff
}
}
/**
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postupdate-postremove-postpersist
*/
public function postUpdate(User $user, PostUpdateEventArgs $event): void
{
}
/**
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postupdate-postremove-postpersist
*/
public function postRemove(User $user, PostRemoveEventArgs $event): void
{
}
/**
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postupdate-postremove-postpersist
*/
public function postPersist(User $user, PostPersistEventArgs $event): void
{
}
/**
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postload
*/
public function postLoad(User $user, PostLoadEventArgs $event): void
{
}
}
Plus sur Stackoverflow Lire la doc Snippet aléatoire