Doctrine 监听器与订阅器 [英] Doctrine Listener versus Subscriber

查看:37
本文介绍了Doctrine 监听器与订阅器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Symfony2 框架中工作,想知道什么时候会使用 Doctrine 订阅者而不是监听者.Doctrine 为听众提供的文档非常清楚,但订阅者却轻轻带过.Symfony 的 cookbook entry 类似.

I'm working in the Symfony2 framework and wondering when would one use a Doctrine subscriber versus a listener. Doctrine's documentation for listeners is very clear, however subscribers are rather glossed over. Symfony's cookbook entry is similar.

推荐答案

在我看来,只有一个主要区别:

From my point of view, there is only one major difference:

  • 注册监听器,指定监听的事件.
  • 订阅者有一个方法告诉调度器它正在监听什么事件

这可能看起来没有什么大的区别,但如果你仔细想想,在某些情况下,你会想要使用一种而不是另一种:

This might not seem like a big difference, but if you think about it, there are some cases when you want to use one over the other:

  • 您可以将一个侦听器分配给许多具有不同事件的调度程序,因为它们是在注册时设置的.您只需要确保每个方法都在侦听器中就位
  • 您可以通过更改 getSubscribedEvents 的返回值来更改订阅者在运行时注册的事件,甚至在注册订阅者之后(想想有一次,您听到一个非常嘈杂的事件,只想执行一次)
  • You can assign one listener to many dispatchers with different events, as they are set at registration time. You only need to make sure every method is in place in the listener
  • You can change the events a subscriber is registered for at runtime and even after registering the subscriber by changing the return value of getSubscribedEvents (Think about a time where you listen to a very noisy event and you only want to execute something one time)

可能还有其他我不知道的差异!

There might be other differences I'm not aware of though!

这篇关于Doctrine 监听器与订阅器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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