Php Symfony2原则侦听器后持久化程序未调用
我一直在遵循示例代码,尝试让我的doctrine事件侦听器工作。然而,即使类被实例化为一个对象(我知道这一点,因为我记录了_构造,并且也调用了_析构函数),postPersist函数永远不会这样做 myservices.yml文件包含以下内容(位于AH/Core/SolutionBundle/Resources/config/services.yml中): (另外,services.yml文件正在加载到AH/Core/SolutionBundle/DependencyInjection/SolutionExtension.php中-已确认,因为其他服务运行正常) 我的实体只是一个标准的条令实体,除了使用一些额外的基于注释的集成(如JMS序列化程序)外,没有什么特别之处。唯一不同于大多数其他实体的是,我们使用标准的SingleTableInheritance from条令,使用@ORM\DiscriminatorMap注释和child-e20多岁 我的监听器现在只有一个骨架,用来测试它是否在没有任何干扰的情况下工作:Php Symfony2原则侦听器后持久化程序未调用,php,symfony,doctrine-orm,doctrine,listener,Php,Symfony,Doctrine Orm,Doctrine,Listener,我一直在遵循示例代码,尝试让我的doctrine事件侦听器工作。然而,即使类被实例化为一个对象(我知道这一点,因为我记录了_构造,并且也调用了_析构函数),postPersist函数永远不会这样做 myservices.yml文件包含以下内容(位于AH/Core/SolutionBundle/Resources/config/services.yml中): (另外,services.yml文件正在加载到AH/Core/SolutionBundle/DependencyInjection/Solu
<?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;
}
}
样本输出:
AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener\uu构造
AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener\uu destruct
我不知道我在这里哪里出了错,它遵循文档超级接近:
我也查了这个单据,跟上面类似:
以下是,如果希望触发事件,则需要刷新更改。不刷新的持久化实体不会生成主键。而且持久化实体不会调用数据库插入操作。不同的方法-为什么不尝试此方法-只需将函数添加到实体并映射到yml/xml或annotati即可在link()上执行
$this->em->flush()
还要实际保留您的实体谢谢@Ziumin是的,我试过了,虽然postPersist应该在刷新之前启动。另外,当尝试时,我仍然有相同的结果。@user5419232是的,我以前做过。不幸的是,我必须将此实现分离出来,以便以后可以删除它。谢谢。Flushing仍然没有调用postPersist函数调用。但是,在解决这个问题的时间用完后,我从使用postPersist切换到使用postUpdate标记,这确实起到了作用。
<?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;
}
}
$cs = $csm->findClientSolutionById(123); // don't worry where $csm comes from
$cs->setUid('do some update: '.rand(0,10000));
$this->em->persist($cs);