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\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
{
}
}
More on Stackoverflow Read the doc Random snippet
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉
