Symfony2 Doctrine Listener postPersist 不调用
Symfony2 Doctrine Listener postPersist not invoking
我一直在按照示例代码尝试让我的学说事件侦听器工作。但是,即使 class 被实例化为对象(我知道这一点是因为我记录了 __construct,并且 __destructor 也被调用,但 postPersist 函数永远不会调用。
我的 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 }
(此外,services.yml 文件正在 AH/Core/SolutionBundle/DependencyInjection/SolutionExtension 中加载。php - 已确认,因为其他服务 运行 正常)
我的实体只是一个标准的学说实体,没有什么特别之处,除了使用一些额外的基于注释的集成,比如 JMS 序列化程序。与大多数其他实体唯一不同的是,我们使用来自 Doctrine 的标准 SingleTableInheritence,使用 @ORM\DiscriminatorMap 注释和子实体。
我的听众现在只有一个骨架,以测试它是否可以在没有任何干扰的情况下工作:
<?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;
}
}
在测试它时,运行使用下面的代码,我只看到 __construct 和 __destruct 运行(通过回显),但看不到坚持:
$cs = $csm->findClientSolutionById(123); // don't worry where $csm comes from
$cs->setUid('do some update: '.rand(0,10000));
$this->em->persist($cs);
示例输出:
AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener __construct
AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener __destruct
我不知道哪里出了问题,它非常接近文档:
Doctrine documentation
我也查了这个文档,跟上面的差不多:
Symfony documentation around listeners
这是 explanation, and the implementation 因此,如果您希望触发事件,则需要刷新更改。持久化实体而不刷新它们不会生成主键。持久化实体也不会调用数据库插入操作。
我一直在按照示例代码尝试让我的学说事件侦听器工作。但是,即使 class 被实例化为对象(我知道这一点是因为我记录了 __construct,并且 __destructor 也被调用,但 postPersist 函数永远不会调用。
我的 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 }
(此外,services.yml 文件正在 AH/Core/SolutionBundle/DependencyInjection/SolutionExtension 中加载。php - 已确认,因为其他服务 运行 正常)
我的实体只是一个标准的学说实体,没有什么特别之处,除了使用一些额外的基于注释的集成,比如 JMS 序列化程序。与大多数其他实体唯一不同的是,我们使用来自 Doctrine 的标准 SingleTableInheritence,使用 @ORM\DiscriminatorMap 注释和子实体。
我的听众现在只有一个骨架,以测试它是否可以在没有任何干扰的情况下工作:
<?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;
}
}
在测试它时,运行使用下面的代码,我只看到 __construct 和 __destruct 运行(通过回显),但看不到坚持:
$cs = $csm->findClientSolutionById(123); // don't worry where $csm comes from
$cs->setUid('do some update: '.rand(0,10000));
$this->em->persist($cs);
示例输出:
AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener __construct AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener __destruct
我不知道哪里出了问题,它非常接近文档: Doctrine documentation
我也查了这个文档,跟上面的差不多: Symfony documentation around listeners
这是 explanation, and the implementation 因此,如果您希望触发事件,则需要刷新更改。持久化实体而不刷新它们不会生成主键。持久化实体也不会调用数据库插入操作。