我有一个活动订阅者:
namespace App\EventListener;
use App\Entity\Token;
use App\Model\EncryptUtils;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Common\EventSubscriber;
class DatabaseSubscriber implements EventSubscriber
{
public function getSubscribedEvents()
{
return array(
'prePersist'
);
}
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof Token) {
$enityt->setCreatedAt(new \DateTime());
}
}
}
我已经在services.yaml中声明了该服务: 参数: 语言环境:“ en”
services:
App\EventListener\DatabaseSubscriber:
tags:
- { name: doctrine.event_subscriber, connection: default }
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$appSecret: '%kernel.secret%'
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
但是无法使其正常工作。我也尝试过使用事件监听器,但是什么也没发生
答案 0 :(得分:2)
您应该在所有默认配置之后声明服务,因为如果没有,默认配置将覆盖您的服务。
因此,您的services.yaml文件应为:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$appSecret: '%kernel.secret%'
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
App\EventListener\DatabaseSubscriber:
tags:
- { name: doctrine.event_subscriber, connection: default }