Doctrine EntityManager在嵌套实体中清除方法 [英] Doctrine EntityManager clear method in nested entities

查看:170
本文介绍了Doctrine EntityManager在嵌套实体中清除方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用原则批量插入处理为了优化大量实体的插入。问题在于Clear方法。它表示该方法分离了由EntityManager管理的所有实体。那么我应该怎么做,我有一个父母实体,有很多孩子,每个孩子都有他们的孩子,如下所示:

I would like to use doctrine batch insert processing in order to optimize insert of a big amount of entities. The problem is with Clear method. It says that this method detach all entities that are managed by EntityManager. So what should I do in a situation where I have a parent Entity, which has many child, and each child has their childs, like this:


  • riseSession

    • track


      所以我有一个rideSession,3个轨道,每个轨道都有等于2 000点。我可以在最后一个循环中使用批处理,负责保存点。但是如果我使用清晰的方法,那么如何设置父母的点和轨迹?清除方法会将它们分开,对吧?

      So I have 1 rideSession, 3 tracks and each track has for isntance 2 000 points. I could use batch processing in last loop which is responsible for saving points. But if I use clear method, then how to set parents for points and tracks? Clear method will detach them, right?

      推荐答案

      很快你会达到内存限制。批量的 flush()的想法是确定的,但您需要 clear() EntityManager 在每个批次结束时释放二进制的内存。

      Soon You will hit memory limit. The idea of flush() in batches is ok, but You need to clear() EntityManager at the end of each batch to release used memory.

      但在你的情况下,你将有 ORMInvalidArgumentException (akanew entity foung through relationship)如果您在 $ child-> setParent($ parent); 之后调用 $ entityManager-> clear();

      But in Your case, You will have ORMInvalidArgumentException (a.k.a. "new entity foung through relationship") if You will call $child->setParent($parent); after You did $entityManager->clear();.

      问题是 $ parent 现在位于 UnitOfWork 中的分离状态。所以你需要再次在管理状态。这可以通过使用 $ entityManager-> merge(); 或简单地使用 $ parentRepository-> find($ parent->的getId());

      Problem is that $parent is now in detached state in UnitOfWork. So You need to put it again in managed state. This can be done by using $entityManager->merge(); or simply by using $parentRepository->find($parent->getId());.

      只需确保在每个 clear()之后,将所有实体调出管理 如果您以后再使用它们。

      Just make sure to bring up all Entities to managed state after each clear() in case You going to use them later.

      这篇关于Doctrine EntityManager在嵌套实体中清除方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆