FOSUserEvents::REGISTRATION_COMPLETED的订阅是运行的两倍
Subscribtion to FOSUserEvents::REGISTRATION_COMPLETED is running twice
我订阅了 symfony 3
FOSUserEvents::REGISTRATION_COMPLETED
用地址数据填充第二 table 并写入日志。
namespace AppBundle\EventListener;
use AppBundle\Entity\Address;
use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterUserResponseEvent;
class RegistrationSubscriber implements EventSubscriberInterface
{
private $em;
private $container;
public function __construct(EntityManager $em,ContainerInterface $container)
{
$this->em = $em;
$this->container = $container;
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_COMPLETED => [
['onRegistrationCompleted',0]
],
];
}
public function onRegistrationCompleted(FilterUserResponseEvent $event)
{
$user = $event->getUser();
$address = new Address();
$address->addAddress($user);
$this->em->persist($address);
$this->em->flush();
$this->container->get('app.logging')->write('Anmeldung',array('customer'=>$user->getId()));
}
}
services.yml:
app.registration_listener:
class: 'AppBundle\EventListener\RegistrationSubscriber'
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: kernel.event_subscriber}
app.logging:
class: AppBundle\Util\LogHandler
arguments: ['@doctrine.orm.entity_manager']
public: true
提交注册表后,触发了FOSUserEvents::REGISTRATION_COMPLETED,但是触发了两次运行,因为地址数据和日志数据都写了两次。
可能是什么原因?
我自己找的
在 symfony 3.3 的 services.yml 中我使用了选项
自动配置:真,
所以服务和事件订阅者会自动加载。
这就是事件被触发两次的原因。
我订阅了 symfony 3
FOSUserEvents::REGISTRATION_COMPLETED 用地址数据填充第二 table 并写入日志。
namespace AppBundle\EventListener;
use AppBundle\Entity\Address;
use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterUserResponseEvent;
class RegistrationSubscriber implements EventSubscriberInterface
{
private $em;
private $container;
public function __construct(EntityManager $em,ContainerInterface $container)
{
$this->em = $em;
$this->container = $container;
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_COMPLETED => [
['onRegistrationCompleted',0]
],
];
}
public function onRegistrationCompleted(FilterUserResponseEvent $event)
{
$user = $event->getUser();
$address = new Address();
$address->addAddress($user);
$this->em->persist($address);
$this->em->flush();
$this->container->get('app.logging')->write('Anmeldung',array('customer'=>$user->getId()));
}
}
services.yml:
app.registration_listener:
class: 'AppBundle\EventListener\RegistrationSubscriber'
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: kernel.event_subscriber}
app.logging:
class: AppBundle\Util\LogHandler
arguments: ['@doctrine.orm.entity_manager']
public: true
提交注册表后,触发了FOSUserEvents::REGISTRATION_COMPLETED,但是触发了两次运行,因为地址数据和日志数据都写了两次。
可能是什么原因?
我自己找的
在 symfony 3.3 的 services.yml 中我使用了选项 自动配置:真, 所以服务和事件订阅者会自动加载。
这就是事件被触发两次的原因。