在我的應用程序中,我有「用戶」。 一個用戶可以有幾個「帳戶」EventListener更新其他實體和無限循環
我在我的「帳戶」實體上有一個監聽器。 正是在這樣的「service.yml」文件中聲明:
account_listener:
class: AppBundle\EventListener\AccountListener
arguments:
- '@service_container'
tags:
- {name: doctrine.event_listener, event: preUpdate}
在我服務的方法更新前:
public function preUpdate(PreUpdateEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if (!$entity instanceof Account) {
return;
}
$this->container->get('notification_manager')->sendNotification();
}
的sendNotification的方法調用嘗試創建的實體的功能「的通知「
public function sendNotification()
{
$notification = new Notification();
$data = array(
'label' => 'Hello'
)
$form_notif = $this->formFactory->create(NotificationType::class, $notification, ['method' => 'POST']);
$form_notif->submit($data,($method === 'POST'));
if ($form_notif->isValid())
{
$this->em->persist($notification);
$this->em->flush();
} else {
return $form_notif;
}
return $notification;
}
問題: 未創建的通知和PHP是ST在無限循環中。
爲了防止這種情況,我添加了這個在sendNotification的方法beggining:
$eventManager = $this->em->getEventManager();
$eventManager->removeEventListener(['preUpdate'],$this->container->get('account_listener'));
有了這個,它的工作原理。但我認爲還有更好的辦法。
你能幫我嗎?
感謝