【问题标题】:Symfony 2 Doctrine event subscribers onFlush/PostFlush don't firing in external bundleSymfony 2 Doctrine 事件订阅者 onFlush/PostFlush 不会在外部包中触发
【发布时间】:2017-02-06 12:31:58
【问题描述】:

当我想与订阅者一起执行一些代码后学说 Flush 时遇到问题。当我更新实体时,我的 on/post flush 函数不会执行。

我在 symfony 日志中没有错误(或其他任何内容),即使我在事件中导致语法错误,nginx/php 日志中也没有任何错误。我重新启动了我的 php 并清除了我的缓存。

作为the symfony doc says for my symfony version (2.8),我做了以下:

MyBundle\EventSubscriber\MyEntityUpdatedEventSubscriber

<?php

namespace MyBundle\EventSubscriber;

use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\Common\EventSubscriber;

class MyEntityEventSubscriber implements EventSubscriber
{
    public function __construct(MySuperService $myService)
    {
        $this->myService = $myService;
    }

    public function getSubscribedEvents()
    {
        return array(
            'onFlush',
            'postFlush',
        );
    }

    public function onFlush(OnFlushEventArgs $args)
    {
        $uow = $args->getEntityManager()->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            // bla bla
        }
        //some interesting code
    }

    public function postFlush()
    {
        //Another interesting lines
    }
}

我的服务声明如下所示:

MyBundle/Resources/config/service.yml

services:
    my_bundle.event.myentity_updated:
        class: MyBundle\EventSubscriber\MyEntityUpdatedEventSubscriber
        arguments:
            - @my_super_service
        tags:
            - { name: doctrine.event_subscriber, connection: default }

最后,我的 service.yml 被加载到包依赖注入扩展中。

MyBundle\DependencyInjection\MyBundleExtension

[...]

    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
[...]

我的问题很简单:我错过了什么吗?

【问题讨论】:

    标签: php symfony events doctrine-orm doctrine


    【解决方案1】:

    我注意到您以onFlushpostFlush 订阅了两个事件。如果您看到文档here,我没有看到任何此类事件。

    但是我总是使用以下事件,它们满足了我的各种要求。

    /**
     * Hook to the events that need to be subscribed!!
     * @return array
     */
    public function getSubscribedEvents()
    {
        return array(
            'prePersist',
            'postPersist',
            'preUpdate',
            'postUpdate',
            'postDelete',
        );
    }
    

    还有一件事: 如果您的服务@my_super_service 需要@doctrine.orm.entity_manager 作为其参数之一,您可能会看到一些infinite dependency loop。刚刚提到过,因为我曾经遇到过。

    【讨论】:

    • 感谢您的回答和建议。这是由教义docs.doctrine-project.org/projects/doctrine-orm/en/latest/… 触发的事件列表。我需要使用这些事件,但我尝试了 LifeCycle (postUpdate) 进行测试。它根本没有传入我的函数。听起来 symfony 不知道我的订阅者...
    • 您可以尝试在app/config/services.yml 中声明您的服务配置吗?以防万一,Bundle dependency injection 没有启动服务。
    • 抱歉这两周没有回复,我有很多事情要做。我今天早上回到了这个问题,我刚刚看到我没有在我的 AppKernel 中声明我的外部包与监听器......抱歉浪费时间,我会在今天下午删除这个问题,因为它不是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2010-12-21
    • 2017-02-07
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 2017-02-01
    相关资源
    最近更新 更多