Skip to content

Instantly share code, notes, and snippets.

@NBoulfroy
Created November 8, 2021 14:11
Show Gist options
  • Save NBoulfroy/2ef4c52bdd847274d809674f92fbb48c to your computer and use it in GitHub Desktop.
Save NBoulfroy/2ef4c52bdd847274d809674f92fbb48c to your computer and use it in GitHub Desktop.
[Doctrine event with data fixtures] Prevent doctrine event with data fixtures #PHP #Symfony #Doctrine
<?php
namespace App\EventListener;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
class UserSubscriber implements EventSubscriberInterface
{
public function getSubscribedEvents(): array
{
return [
Events::prePersist
];
}
public function prePersist(LifecycleEventArgs $args): void
{
// Check the type of interface (the Server API, SAPI) that PHP is using.
if ('cli' === php_sapi_name()) {
return;
}
$entity = $args->getEntity();
if (!$entity instanceof User) {
return;
}
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment