在Flush上持久保存新实体

2024-03-05

我尝试使用onFlushDoctrine 中的事件会持久化一个新实体,但在尝试持久化时会导致无限循环。这是我在侦听器中执行的操作:

$countusers = $em->getRepository('DankeForumBundle:NotificationUser')->countNotificationsByDeal($entity);
if ($countusers > 0) {
  $notification = new NotificationAction();
  $notification->setDeal($entity);
  $notification->setDatepost(new \DateTime());
  $notification->setNotificationtype(NotificationAction::TYPE_TOP_DEAL);
  // $em is set to EntityManager
  $em->persist($notification);
  // $uow ist set to UnitOfWork
  $uow->computeChangeSet($em->getClassmetadata('Danke\ForumBundle\Entity\NotificationAction'), $notification);
}

我知道当我在水里冲水时我会陷入循环onFlush事件,但我不这样做!我只按照文档中的说明计算新的更改集。

有人能告诉问题出在哪里吗?


我对 onFlush 事件也有类似的问题。请更换

$em->persist($notification);

to

$uow = $em->getUnitOfWork();
$uow->persist($notification);

$metadata = $em->getClassMetadata(get_class($notification));
$uow->computeChangeSet($metadata, $notification);

$uow->persist()将使工作单元了解新实体并安排其插入。 呼唤$uow->computeChangeSet()收集 Doctrine 持久器应插入的数据是必要的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在Flush上持久保存新实体 的相关文章

随机推荐