嗨,我有一个 prePersist 和 preUpdate 侦听器:
<?php
namespace FM\AppBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use FM\AdminBundle\Entity\Address\DeliveryAddress;
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(); …
Run Code Online (Sandbox Code Playgroud) 我一直在努力解决烦人的问题.我正在尝试创建与InventoryItems过期关联的通知实体.
这些InventoryItems是为用户自动生成的,但用户可以编辑它们并单独设置它们的到期日期.保存后,如果InventoryItem具有到期日期,则生成通知实体并将其关联.因此,当实体更新时会创建这些通知实体,因此onPersist事件不起作用.
一切似乎都运行良好,并在按预期保存InventoryItems时生成通知.唯一的问题是,当第一次创建通知时,即使它已正确保存,也不会保存对InventoryItem的更改.即创建了具有正确到期日期的通知,但有效期未保存在InventoryItem上.
这是我的onFlush代码:
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityUpdates() as $entity) {
if ($entity instanceof NotificableInterface) {
if ($entity->generatesNotification()){
$notification = $this->notificationManager->generateNotificationForEntity($entity) ;
if ( $notification ) {
$uow->persist($notification) ;
}
$entity->setNotification($notification) ;
$uow->persist($entity);
$uow->computeChangeSets();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该问题仅在通知第一次与实体关联时发生,即第一次在InventoryItem上设置到期日期.在更新到期日期的后续实例中,更新将在Notification和InventoryItem上正确反映.
任何想法或建议将不胜感激.
谢谢