2014-09-04 49 views
0

我有一个经理和一个方法。 在此方法中,我想添加一个监听器连接到教义和使用:Symfony2:addListener不使用EventSubscriber类

$dispatcher = $this->container->get("event_dispatcher"); 
$dispatcher->addListener(Events::onFlush, function (Event $event) { 
    var_dump('test');die; 
}); 

但在这种情况下,没有追加。 你有解决方案来完成这个吗?我读了很多关于收听时间的文件,但我不明白为什么没有工作:/ (我不希望使用EventSubscriber类)

谢谢

回答

1

您的代码似乎不错,您必须使用教义事件调度程序,而不是Symfony调度程序。

$doctrineEventManager = $this->em->getEventManager(); 
$doctrineEventManager->addEventListener(Events::onFlush, New YourEventListenerClass()});; 
+0

这是我的尝试之一,但这样做我有这样的错误:“致命错误:调用未定义的方法关闭:: onFlush()”:当然应该闭幕写得很好的/ – Draeli 2014-09-04 10:24:30

+0

是,给一个请看文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush。 '''公共函数onFlush(OnFlushEventArgs $ eventArgs)''' – skler 2014-09-04 10:28:48

+0

你是真实的,但在这种情况下,就像做一个“EventSubscriber类”,这是我想避免:( – Draeli 2014-09-04 10:37:48