Created
November 8, 2021 14:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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