坚持新实体onFlush

问题描述 投票:5回答:1

我尝试在Doctrine中使用onFlush事件来持久保存新实体,但在尝试持久化时会导致无限循环。这是我在Listener中所做的:

$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事件中冲洗时,我会得到一个循环,但我不这样做!我只计算文档中所说的新变更集。

有人可以告诉问题在哪里吗?

编辑:可能有趣的是,我确信它在几天前有效,但我记不起改变任何东西(我知道这不可能是真的;))...

events doctrine doctrine-orm flush persist
1个回答
3
投票

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

$em->persist($notification);

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

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

$uow->persist()将使UnitOfWork了解新实体并将其安排进行插入。调用$uow->computeChangeSet()是必要的,以收集应该由Doctrine的persister插入的数据。

© www.soinside.com 2019 - 2024. All rights reserved.