我尝试使用 onFlush
Doctrine 中的事件以持久化一个新实体,但在尝试持久化时会导致无限循环。这是我在监听器中所做的:
$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);
到
$uow = $em->getUnitOfWork();
$uow->persist($notification);
$metadata = $em->getClassMetadata(get_class($notification));
$uow->computeChangeSet($metadata, $notification);
$uow->persist()
将使 UnitOfWork 知道新实体并安排它插入。调用
$uow->computeChangeSet()
是必要的,以收集应由 Doctrine 的持久器插入的数据。
关于php - 持久化新实体 onFlush,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10796496/