【发布时间】: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