坚持新实体onFlush [英] persist new entity onFlush
问题描述
我尝试在Doctrine中使用onFlush事件来持久化一个新实体,但是在尝试持久化时会导致无限循环。这是我在侦听器中执行的操作:
I try to user the onFlush Event in Doctrine to persist a new entity, but it leads to an infinite loop when trying to persist. Here is what I do in the 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中进行刷新时会出现循环活动,但我不这样做!我只能按照文档中的说明来计算新的变更集。
I know that I would get a loop, when I was flushing in the onFlush Event, but I don't do that! I only compute the new change set as it says in the documentation.
有人可以告诉问题出在哪里吗?
Can someone tell where the problem is?
编辑:也许很有趣,我确定它在几天前就可以使用,但我不记得更改任何内容(我知道这是不对的;))...
It maybe interesting, that I am sure it worked some days ago, but I can't remember changing anything (which I know can't be true ;) )...
推荐答案
我的onFlush事件有类似的问题。请更改
I had similar issues with onFlush Event. Please change
$em->persist($notification);
到
$uow = $em->getUnitOfWork();
$uow->persist($notification);
$metadata = $em->getClassMetadata(get_class($notification));
$uow->computeChangeSet($metadata, $notification);
$ ow-> persist()
将使 UnitOfWork 了解新实体,并安排其插入。
调用 $ uow-> computeChangeSet()
是必要的,以收集应该由教义的坚持者插入的数据。
$uow->persist()
will make the UnitOfWork know about the new entity and will schedule it for insertion.
Calling $uow->computeChangeSet()
is neccessary, to collect the data that should be inserted by Doctrine's persister.
这篇关于坚持新实体onFlush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!