Doctrine EntityManager在嵌套实体中清除方法 [英] Doctrine EntityManager clear method in nested entities
问题描述
我想使用原则批量插入处理为了优化大量实体的插入。问题在于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 toclear()
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 indetached
state inUnitOfWork
. So You need to put it again inmanaged
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 eachclear()
in case You going to use them later.这篇关于Doctrine EntityManager在嵌套实体中清除方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- track