【问题标题】:Symfony event listener service sharedSymfony 事件监听器服务共享
【发布时间】:2022-12-26 04:22:25
【问题描述】:

我有事件侦听器作为服务,声明为:

#[Autoconfigure(shared: true)]
#[AutoconfigureTag(name: 'doctrine.event_listener', attributes: [
    'event' => Events::preUpdate,
    'entity' => User::class,
    'method' => 'preUpdate',
])]
#[AutoconfigureTag(name: 'doctrine.event_listener', attributes: [
    'event' => Events::postFlush,
    'method' => 'postFlush',
])]
class Sender

不幸的是,symfony 为 preUpdate 和 postFlush 创建了另一个对象,所以我不能使用 shared this。

它出什么问题了?

谢谢。

【问题讨论】:

    标签: php symfony event-listener


    【解决方案1】:

    你创建了两个同名的别名,symfony 会选择第一个。 我相信它应该是这样的:

    #[AutoconfigureTag(name: 'doctrine.event_listener.preUpdate', attributes: [
        'entity' => User::class,
        'method' => 'preUpdate',
    ])]
    #[AutoconfigureTag(name: 'doctrine.event_listener.postFlush', attributes: [
        'method' => 'postFlush',
    ])]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 2011-09-28
      • 2014-07-27
      • 1970-01-01
      • 2019-07-06
      • 2016-07-09
      • 1970-01-01
      相关资源
      最近更新 更多