注册新的Doctrine事件订阅者时的Symfony异常(SoftDeleteable) [英] Symfony exception when registering new Doctrine event subscriber (SoftDeleteable)

查看:150
本文介绍了注册新的Doctrine事件订阅者时的Symfony异常(SoftDeleteable)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symphony 2.3与Sonata管理软件包, DoctrineExtensions (由 StofDoctrineExtensionsBundle )。我启用,配置并成功测试了SoftDeleteable和Timestampable组件。

I'm using Symfony 2.3 with Sonata Admin Bundle with DoctrineExtensions (which is enabled by StofDoctrineExtensionsBundle). I enabled, configured and successfully tested SoftDeleteable and Timestampable components.

现在,当我尝试使用Symfony标记的服务添加另一个Doctrine事件订阅者时,似乎是softdeleteable的监听器禁用。

Now, when I try adding another Doctrine event subscriber using Symfony tagged service, it seems as the softdeleteable listener is being disabled.

我的服务:

My service:

my.contact.listener.tag:
    class: My\ContactBundle\EventListener\TagListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setTagManager, [ @fpn_tag.tag_manager ] ]

订阅者:

namespace My\ContactBundle\EventListener;


use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use DoctrineExtensions\Taggable\Taggable;
use FPN\TagBundle\Entity\TagManager;

class TagListener implements EventSubscriber
{
    /**
     * @var TagManager
     */
    private $tagManager;

    /**
     * @param \FPN\TagBundle\Entity\TagManager $tagManager
     */
    public function setTagManager($tagManager)
    {
        $this->tagManager = $tagManager;
    }

    /**
     * Load tags for Taggable entities
     *
     * @param LifecycleEventArgs $args
     */
    public function postLoad(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if ($entity instanceof Taggable) {
            $this->tagManager->loadTagging($entity);
        }
    }

    /**
     * Save tags for Taggable entities
     *
     * @param LifecycleEventArgs $args
     */
    public function preUpdate(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if ($entity instanceof Taggable) {
            $this->tagManager->saveTagging($entity);
        }
    }

    /**
     * Save tags for Taggable entities
     *
     * @param LifecycleEventArgs $args
     */
    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if ($entity instanceof Taggable) {
            $this->tagManager->saveTagging($entity);
        }
    }

    public function getSubscribedEvents()
    {
        return array(
            'prePersist',
            'preUpdate',
            'postLoad',
        );
    }
}

在每个请求中我收到异常:

In each request I get exception:

Listener "SoftDeleteableListener" was not added to the EventManager!

如果我禁用我的订阅者,问题就消失了。

If I disable my subscriber, the problem is gone. How to enable my event subscriber and have softdeleteable too?

推荐答案

我今天遇到了同样的问题。

I got the same problem as you do today.

问题是 fpn_tag.tag_manager 取决于 doctrine.orm.default_entity_manager ,但 TagListener doctrine.orm.default_entity_manager 的依赖项,如果您使用 doctrine.event_subscriber 。因此创建循环依赖。但是服务容器没有检测到,而是尝试在之后添加事件,返回原则连接服务。查看更多详细信息 here

The problem is that fpn_tag.tag_manager depends on doctrine.orm.default_entity_manager, but the TagListener is a dependency of doctrine.orm.default_entity_manager if you tag it with doctrine.event_subscriber. Thus creating a circular dependency. But this is not detected by the service container, instead it tries to add the events after the doctrine connection service is returned. See more details here.

有两种方法可以解决这个问题。

There are two ways to fix this


  1. 您可以将服务容器注入到 TagListener ,然后根据需要加载 fpn_tag.tag_manager

  2. 上创建一个监听器kernel.request 事件,然后手动将事件订阅者添加到实体管理器。

  1. You can inject the service container into the TagListener, then load fpn_tag.tag_manager on demand.
  2. Create a listener on kernel.request event, then manually add the event subscriber to the entity manager.

一个附注,我建议反对在 preUpdate prePersist 事件之间调用 saveTagging 因为 saveTagging 会执行一个隐式刷新,这是不安全的调用这些事件。

A side note, I'd recommend against calling saveTagging inside preUpdate and prePersist events. Because saveTagging does a implicit flush, which is not safe to call in these events.

这篇关于注册新的Doctrine事件订阅者时的Symfony异常(SoftDeleteable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆