尝试注册 Doctrine EventSubscriber 但不起作用。
(symfony 7)
这是订阅者:
<?php
namespace App\EventSubscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
class LogSubscriber implements EventSubscriber
{
public function getSubscribedEvents()
{
return [
Events::postPersist,
Events::postUpdate,
Events::postRemove
];
}
public function postPersist(LifecycleEventArgs $args)
{
dd($args);
}
public function postUpdate(LifecycleEventArgs $args)
{
dd($args);
}
public function postRemove(LifecycleEventArgs $args)
{
dd($args);
}
}
和我的 services.yaml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\EventSubscriber\LogSubscriber:
tags:
- { name: doctrine.orm.event_subscribe, connection: default }
我尝试使用“doctrine.event_subscribe”,但这不起作用。
我真的不明白出了什么问题,提前感谢您的帮助。
正如 @yolenoyer 评论的那样,该界面已被弃用。
为您的监听器添加新的 PHP 8 属性
<?php
declare(strict_types=1);
namespace App\EventListener;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostRemoveEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Event\PreRemoveEventArgs;
use Doctrine\ORM\Events;
#[AsDoctrineListener(event: Events::prePersist, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::postPersist, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::postRemove, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::postUpdate, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::preRemove, priority: 0, connection: 'default')]
class EventListener
{
public function prePersist(PrePersistEventArgs $event): void
{
$this->logger->debug('-- PrePersistListener::PREPERSIST --');
}
public function postPersist(PostPersistEventArgs $event): void
{
$this->logger->debug('-- PostPersistListener::POSTPERSIST --');
}
public function postRemove(PostRemoveEventArgs $event): void
{
$this->logger->debug('-- PostRemoveListener::POSTREMOVE --');
}
public function postUpdate(PostUpdateEventArgs $event): void
{
$this->logger->debug('-- PostUpdateListener::POSTUPDATE --');
}
/**
* You need to listen to preRemove if you use soft delete
* from Doctrine extensions, because it prevents postRemove
* from being called.
*/
public function preRemove(PreRemoveEventArgs $event): void
{
$this->logger->debug('-- PreRemoveListener::PREREMOVE --');
}
}