Doctrine EventListener onFlush不保存原始实体 [英] Doctrine EventListener onFlush does not save original entity

查看:360
本文介绍了Doctrine EventListener onFlush不保存原始实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在努力解决一个令人讨厌的问题。我正在尝试创建与InventoryItems关联的通知实体到期。



这些InventoryItem是为用户自动生成的,但用户可以对它们进行编辑,并将其到期日期单独设置。保存后,如果InventoryItem具有到期日,则生成Notification实体并将其关联。所以这些通知实体是在一个实体更新时创建的,因此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-> generateNotification()){
$ notification = $ this-> notificationManager-> generateNotificationForEntity($ entity);
if($ notification){
$ uow-> persist($ notification);
}
$ entity-> setNotification($ notification);
$ uow-> persist($ entity);
$ uow-> computeChangeSets();
}
}
}
}

问题仅在首次将通知与实体相关联时发生,即首次在InventoryItem上设置到期日。在以后的情况下,如果更新到期日,则更新会正确地反映在Notification和InventoryItem上。



任何想法或建议将不胜感激。



谢谢

解决方案

你需要在你的电脑上调用computeChangeset 新创建或更新的实体。只要调用computeChangeSets是不够的。

  $ metaData = $ em-> getClassMetadata('Your\NameSpace\Entity\\ \\YourNotificationEntity'); 
$ uow-> computeChangeSet($ metaData,$ notification);


I've been struggling with an annoying issue for a while. I'm trying to create Notification entities associated to InventoryItems that expire.

These InventoryItems are generated automatically for users, but users can edit them and set expiry date on them individually. Upon saving, if an InventoryItem has an expiry date, a Notification entity is generated and associated to it. So these Notification entities are created when an entity is updated and hence onPersist event is not going to work.

Everything seems to work fine and Notifications are generated upon saving InventoryItems as expected. Only problem is, when a Notification is created for the first time, even though it's saved properly, changes to the InventoryItem are not saved. That is a Notification with correct expiry date is created, but the expiry date is not saved on the InventoryItem.

Here's my onFlush code:

 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();
          }
        }
    }
}

The problem only occurs the first time a Notification is associated to an entity, i.e. the first time an expiry date is set on an InventoryItem. In later instances when expiry date is updated, the update is reflected correctly on both the Notification and InventoryItem.

Any ideas or suggestions would be appreciated.

Thanks

解决方案

You need to call computeChangeset specifically on your newly created or updated entity. Just calling computeChangeSets is not enough.

    $metaData = $em->getClassMetadata('Your\NameSpace\Entity\YourNotificationEntity');
    $uow->computeChangeSet($metaData, $notification);

这篇关于Doctrine EventListener onFlush不保存原始实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆