Using doctrine/doctrine-bundle in Nettrine

lookass
Member | 54
+
0
-

Hello, I have a Nette project with Doctrine ORM (Nettrine) and I have a question if there is some integration of doctrine/doctrine-bundle with Nettrine? I am missing using Entity Listener.

Thanks for any advice.

Marek Bartoš
Nette Blogger | 1176
+
0
-

Doctrine bundle is for Symfony, not for Nette.

lookass
Member | 54
+
0
-

Marek Bartoš wrote:

Doctrine bundle is for Symfony, not for Nette.

Understand, maybe I used bad topic. What I am looking for is possibility to catch any Entity event in Nettrine.

nightfish
Member | 474
+
+2
-

@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
+
0
-

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.