简体   繁体   English

Symfony2主义侦听器postPersist不调用

[英]Symfony2 Doctrine Listener postPersist not invoking

I've been following example code to try and get my doctrine event listener to work. 我一直在遵循示例代码来尝试使我的学说事件监听器正常工作。 However, even though the class is being instantiated into an object (I know this because I logged the __construct, and the __destructor also gets invoked, the postPersist function never does. 但是,即使将类实例化为一个对象(我知道这一点是因为我记录了__construct,并且也调用了__destructor,但postPersist函数从不执行。

My services.yml file has the following (located in AH/Core/SolutionBundle/Resources/config/services.yml): 我的services.yml文件具有以下内容(位于AH / Core / SolutionBundle / Resources / config / services.yml中):

solutions_core_reverse_sync:
    class: AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener
    arguments: [@service_container]
    tags:
        - { name: doctrine.event_listener, event: postPersist }

(Also, the services.yml file is being loaded in AH/Core/SolutionBundle/DependencyInjection/SolutionExtension.php - confirmed because other services are running just fine) (此外,services.yml文件正在AH / Core / SolutionBundle / DependencyInjection / SolutionExtension.php中加载-已确认,因为其他服务正在正常运行)

My Entity is just a standard doctrine entity, nothing special about it, except for using a few extra annotation based integrations, like the JMS Serializer. 我的实体只是一个标准的学说实体,除了使用一些额外的基于注释的集成(例如JMS序列化器)外,没有什么特别的。 The only thing that is different to most other entities, is the fact that we use the standard SingleTableInheritence from Doctrine, using an @ORM\\DiscriminatorMap annotation and the child-entities. 与大多数其他实体唯一不同的是,我们使用来自Doctrine的标准SingleTableInheritence,并使用@ORM \\ DiscriminatorMap批注和子实体。

My listener only has a skeleton right now, to test whether it works without anything interfering: 我的侦听器现在只有一个骨架,以测试它是否可以正常工作而没有任何干扰:

<?php
namespace AH\Core\SolutionBundle\Listener;

use Symfony\Component\DependencyInjection\Container;
use Doctrine\ORM\Event\LifecycleEventArgs;

class ClientSolutionReverseSyncListener
{
    protected $container;

    public function __construct(Container $container)
    {
        $this->container = $container;

        echo __CLASS__.' __construct'.PHP_EOL;
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        echo __CLASS__.' postPersist fired'.PHP_EOL;
    }

    public function __destruct()
    {
        echo __CLASS__.' __destruct'.PHP_EOL;
    }
}

When testing it, and running the below code, I only see the __construct and the __destruct run (by way of echoing) but not the postPersist: 在测试它并运行下面的代码时,我只看到__construct和__destruct运行(通过回显),而没有看到postPersist:

$cs = $csm->findClientSolutionById(123); // don't worry where $csm comes from
$cs->setUid('do some update: '.rand(0,10000));
$this->em->persist($cs);

Sample output: 样本输出:

AH\\Core\\SolutionBundle\\Listener\\ClientSolutionReverseSyncListener __construct AH\\Core\\SolutionBundle\\Listener\\ClientSolutionReverseSyncListener __destruct AH \\ Core \\ SolutionBundle \\ Listener \\ ClientSolutionReverseSyncListener __构造AH \\ Core \\ SolutionBundle \\ Listener \\ ClientSolutionReverseSyncListener __构造

I'm at a loss of where I went wrong here, it follows the docs super close: Doctrine documentation 我迷失了我在这里出错的地方,它紧随文档超级关闭: 学说文档

I also checked this doc, which is similar to the above: Symfony documentation around listeners 我还检查了此文档,该文档与上面的文档类似: 围绕侦听器的Symfony文档

Here is the explanation , and the implementation So, you need to flush the changes, if you want the event to be fired. 这是说明 ,以及实现。因此,如果要触发事件,则需要刷新更改。 Persisting entities without flushing them doesn't generate primary key. 持久实体而不刷新它们不会生成主键。 Also persisting entities doesn't call the database insert operations. 同样,持久实体不会调用数据库插入操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM