Symfony 2:在ContainerAwareCommand中禁用主义事件侦听器 [英] Symfony 2: Disable Doctrine event listener in ContainerAwareCommand

查看:83
本文介绍了Symfony 2:在ContainerAwareCommand中禁用主义事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用几个在配置文件中注册的Doctrine侦听器进行一些自动更新(created_on,updated_on时间戳等)。
当前,我已经实现了其他功能,这些功能要求将准备好的值存储在数据库中以便于搜索。

I am using several Doctrine listeners registered in configuration file for some automatic updates (created_on, updated_on timestamps etc.). Currently I have implemented additional functionality that requires stashing prepared values in the database for easier searching.

我正在考虑使用更新的Symfony命令来准备这些值SQL更新脚本(实际上,以数值创建方式进行的任何类型的更改或更新都比仅需要运行此单个命令)。但这也会触发前面提到的EventListeners。

I am thinking about update Symfony command that would prepare these values instead of SQL update script (actually any sort of change or update in the way the value is crated would than require just running this single command). However this would also trigger the EventListeners mentioned earlier.

有没有一种方法可以为单个Command禁用特定的EventLister?

Is there a way how to disable particular EventLister for single Command?

推荐答案

类似的东西应该可以解决问题:

something like this should do the trick :

$searchedListener = null;
$em = $this->getDoctrine()->getManager();
foreach ($em->getEventManager()->getListeners() as $event => $listeners) {
    foreach ($listeners as $key => $listener) {
        if ($listener instanceof ListenerClassYouLookFor) {
            $searchedListener = $listener;
            break 2;
        }
    }
}
if ($searchedListener) {
    $evm = $em->getEventManager();
    $evm->removeEventListener(array('onFlush'), $searchedListener);
}
else { //listener not found

}

这篇关于Symfony 2:在ContainerAwareCommand中禁用主义事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆