事件监听器
与 实体监听器 相反,事件监听器是监听应用程序中所有实体的服务。
有关事件监听器的更多信息,请参阅 https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#implementing-event-listeners。
要注册一个服务作为事件监听器,你必须用 doctrine.event_listener
标签标记它
从 Doctrine bundle 2.8 开始,你可以使用 AsDoctrineListener
属性来标记服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// src/App/EventListener/SearchIndexer.php
namespace App\EventListener;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
#[AsDoctrineListener('postPersist'/*, 500, 'default'*/)]
class SearchIndexer
{
public function postPersist(LifecycleEventArgs $event): void
{
// ...
}
}
本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可协议获得许可。