pmk*_*k1c 6 events doctrine flush persist doctrine-orm
我尝试在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);
}
Run Code Online (Sandbox Code Playgroud)
我知道当我在onFlush事件中冲洗时,我会得到一个循环,但我不这样做!我只计算文档中所说的新变更集.
有人可以告诉问题在哪里吗?
编辑:可能有趣的是,我确信它在几天前有效,但我记不起改变任何东西(我知道这不可能是真的;))...
我对 onFlush 事件有类似的问题。请更换
$em->persist($notification);
Run Code Online (Sandbox Code Playgroud)
到
$uow = $em->getUnitOfWork();
$uow->persist($notification);
$metadata = $em->getClassMetadata(get_class($notification));
$uow->computeChangeSet($metadata, $notification);
Run Code Online (Sandbox Code Playgroud)
$uow->persist()将使UnitOfWork知道新实体并安排它插入。调用$uow->computeChangeSet()是必要的,以收集应由 Doctrine 的持久器插入的数据。
| 归档时间: |
|
| 查看次数: |
2321 次 |
| 最近记录: |