Using doctrine/doctrine-bundle in Nettrine
- nightfish
- Member | 508
@lookass Just implement it by yourself:
EntityManager.php
(register event subscriber):
class EntityManager extends \Nettrine\ORM\EntityManagerDecorator
{
public function __construct(
EntityManagerInterface $wrapped,
LifecycleEventSubscriberFactory $lifecycleEventSubscriberFactory,
) {
parent::__construct($wrapped);
$wrapped->getEventManager()->addEventSubscriber($lifecycleEventSubscriberFactory->create());
}
nettrine.neon
(register overridden event manager to DI):
nettrine.orm:
entityManagerDecoratorClass: Namespace\To\Your\EntityManager
LifecycleEventSubscriberFactory.php
(Nette-generated
factory):
interface LifecycleEventSubscriberFactory
{
public function create(): LifecycleEventSubscriber;
}
LifecycleEventSubscriber.php
(subscriber implementation):
class LifecycleEventSubscriber implements \Doctrine\Common\EventSubscriber
{
private array $logsToPersist = [];
public function getSubscribedEvents(): array
{
return [
\Doctrine\ORM\Events::preRemove,
\Doctrine\ORM\Events::preUpdate,
\Doctrine\ORM\Events::postUpdate,
\Doctrine\ORM\Events::postPersist,
// other events that you want to subscribe to
];
}
// method name needs to match event name
// each method has its dedicated `EventArgs` type for argument
public function preRemove(\Doctrine\ORM\Event\PreRemoveEventArgs $args): void {
// process event
}
- lookass
- Member | 54
nightfish wrote:
@lookass Just implement it by yourself:
EntityManager.php
(register event subscriber):class EntityManager extends \Nettrine\ORM\EntityManagerDecorator { public function __construct( EntityManagerInterface $wrapped, LifecycleEventSubscriberFactory $lifecycleEventSubscriberFactory, ) { parent::__construct($wrapped); $wrapped->getEventManager()->addEventSubscriber($lifecycleEventSubscriberFactory->create()); }
nettrine.neon
(register overridden event manager to DI):nettrine.orm: entityManagerDecoratorClass: Namespace\To\Your\EntityManager
LifecycleEventSubscriberFactory.php
(Nette-generated factory):interface LifecycleEventSubscriberFactory { public function create(): LifecycleEventSubscriber; }
LifecycleEventSubscriber.php
(subscriber implementation):class LifecycleEventSubscriber implements \Doctrine\Common\EventSubscriber { private array $logsToPersist = []; public function getSubscribedEvents(): array { return [ \Doctrine\ORM\Events::preRemove, \Doctrine\ORM\Events::preUpdate, \Doctrine\ORM\Events::postUpdate, \Doctrine\ORM\Events::postPersist, // other events that you want to subscribe to ]; } // method name needs to match event name // each method has its dedicated `EventArgs` type for argument public function preRemove(\Doctrine\ORM\Event\PreRemoveEventArgs $args): void { // process event }
That's exactly what I looked for. Thank you.