Question: how to trigger Doctrine lifecycle events in my code, having the entity data available?
postPersist
and postUpdate
events. Thank you in advance for your help.
Extending @Cerad answer, here's a very basic sample code to achieve the result (trigger a Doctrine LifeCycle event). This sample assumes we're in a Symfony controller:
use Doctrine\ORM\Event\LifecycleEventArgs;
// ...
$user = new AppBundle\Entity\User();
// ... do something with the user
$entityManager = $this->getDoctrine()->getManager();
$eventManager = $entityManager->getEventManager();
$eventArgs = new LifecycleEventArgs($user, $entityManager);
$eventManager->dispatchEvent(\Doctrine\ORM\Events::postPersist, $eventArgs);
Assuming you have access to the entity manager then:
$eventManager = $entityManager->getEventManager();
After that you can build and dispatch events per the documentation.
I checked to see if there is a predefined service but did not see one. But you probably define one as a factory and inject it as needed.
Hope this is what you are asking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With