Symfony EventSubscribe 实体

Symfony EventSubscribe on Entity

正在尝试为实体操作 (CRUD) 创建订阅者,但无法弄清楚。

我知道有一种方法,我可以让监听器向他发送 3 个不同的事件,但这不是我想要达到的,我什至认为这不是好的解决方案。

事件订阅者

<?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 <davidjungman.web@gmail.com>
 */
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();
        }
    }
}

和我的Service.yaml部分

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

我试过了:

我查看了这两个官方教程: -https://symfony.com/doc/current/event_dispatcher.html -https://symfony.com/doc/current/doctrine/event_listeners_subscribers.html

但都没有帮助.. 当我使用配置的显示部分时,我的电脑死机了。

当我尝试调试它时,我可以看到这些方法处于活动状态 ( php bin/console debug:event-调度员 )

但他们正在监听 "event" 事件

Doctrine 有自己的 events handler/subscriber 系统。但是,您正在实施的 class Symfony\Component\EventDispatcher\EventSubscriberInterface; 来自 Symfony 事件系统。

<?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
        }
    }
}