David Jungman
David Jungman

Reputation: 13

Symfony EventSubscribe on Entity

trying to make an subscriber for Entity actions (CRUD) and cannot figure it out.

I know there is a way, where I can make listener and send him 3 different events, but that's not what I want to reach, I dont even think is good solution.

Event Subscriber

<?php

namespace App\EventListener;


use App\Entity\Log;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

/**
 * Part of program created by David Jungman
 * @author David Jungman <[email protected]>
 */
class EntitySubscriber implements EventSubscriberInterface
{
    /**
     * @var EntityManagerInterface
     */
    private $em;

    /**
     * @var TokenStorageInterface
     */
    private $tokenStorage;

    public function __construct(TokenStorageInterface $tokenStorage, EntityManagerInterface $em)
    {
        $this->em = $em;
        $this->tokenStorage = $tokenStorage;
    }

    public static function getSubscribedEvents()
    {
        return array(
            Events::postPersist,
            Events::postUpdate,
            Events::postRemove,
        );
    }

    public function postUpdate(LifecycleEventArgs $args)
    {
        $this->logEvent($args, "remove");
    }

    public function postRemove(LifecycleEventArgs $args)
    {
        $this->logEvent($args, "remove");
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        $this->logEvent($args, "create");
    }

    private function logEvent(LifecycleEventArgs $args, string $method)
    {
        $entity = $args->getEntity();
        if($entity->getShortName() != "Log")
        {
            $user = $this->tokenStorage->getToken()->getUser();
            $log = new Log();

            $log
                ->setUser($user)
                ->setAffectedTable($entity->getShortName())
                ->setAffectedItem($entity->getId())
                ->setAction($method)
                ->setCreatedAt();

            $this->em->persist($log);
            $this->em->flush();
        }
    }
}

and my Service.yaml part

App\EventListener\EntitySubscriber:
    tags:
    - { name: doctrine.event_subscriber, connection: default }

I have tried:

I've looked into these 2 official tutorials: -https://symfony.com/doc/current/event_dispatcher.html -https://symfony.com/doc/current/doctrine/event_listeners_subscribers.html

but neither helped.. when I use shown part of config, my computer freeze.

When I try to debug it, I can see these methods active ( php bin/console debug:event-dispatcher )

but they are listening on "event" event

Upvotes: 0

Views: 1169

Answers (1)

Alister Bulman
Alister Bulman

Reputation: 35167

Doctrine has it's own events handler/subscriber system. However, with the class Symfony\Component\EventDispatcher\EventSubscriberInterface; that you are implementing, that is from the Symfony event system.

<?php
use Doctrine\ORM\Events;
use Doctrine\Common\EventSubscriber;  // **the Doctrine Event subscriber interface**
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;

class MyEventSubscriber implements EventSubscriber
{
    public function getSubscribedEvents()
    {
        return array(
            Events::postUpdate,
        );
    }

    public function postUpdate(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();
        $entityManager = $args->getObjectManager();

        // perhaps you only want to act on some "Product" entity
        if ($entity instanceof Product) {
            // do something with the Product
        }
    }
}

Upvotes: 1

Related Questions