嗨,我有一个预科师和preupdate侦听器:
<?php
namespace FMAppBundleEventListener;
use DoctrineORMEventLifecycleEventArgs;
use FMAdminBundleEntityAddressDeliveryAddress;
class DeliveryAddressListener
{
/**
* @param LifecycleEventArgs $args
*/
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if(!$entity instanceof DeliveryAddress){
return;
}
$this->addNameToUser($args);
$this->addPostalToUser($args);
}
/**
* @param LifecycleEventArgs $args
*/
public function preUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if(!$entity instanceof DeliveryAddress){
return;
}
$this->addPostalToUser($args);
}
/**
* @param LifecycleEventArgs $args
*/
public function addNameToUser(LifecycleEventArgs $args)
{
/** @var DeliveryAddress $deliveryAdress */
$deliveryAdress = $args->getEntity();
$user = $deliveryAdress->getOwner();
if(empty($user->getFirstName())) $user->setFirstName($deliveryAdress->getFirstName());
if(empty($user->getLastName())) $user->setLastName($deliveryAdress->getLastName());
}
/**
* @param LifecycleEventArgs $args
*/
public function addPostalToUser(LifecycleEventArgs $args)
{
/** @var DeliveryAddress $deliveryAdress */
$deliveryAdress = $args->getEntity();
$user = $deliveryAdress->getOwner();
$user->setPostalCode($deliveryAdress->getZipCode());
}
}
service.yml:
delivery_address.listener:
class: FMAppBundleEventListenerDeliveryAddressListener
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: preUpdate }
预科派工作正常。但是,预定期不会持续任何东西。
当我在我的 addpostalTouser()函数中添加此代码时,我的nginx服务器崩溃:
$args->getEntityManager()->persist($deliveryAdress);
$args->getEntityManager()->flush();
您的问题是在此事件中不允许更改更新实体的关联。因此,更改交货address的用户协会将不起作用。
请参阅文档
您可以使用onflush侦听器来做到这一点 - 这是更多的工作,但会做您追求的事情。