在刷新上保留新实体


persist new entity onFlush

我尝试使用教义中的onFlush事件来持久化一个新实体,但在尝试持久化时会导致无限循环。这是我在侦听器中所做的:

$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 Event 上遇到了类似的问题。请更改

$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 的持久器插入的数据。