TL明确的实体; DR:我如何使用Doctrine\ORM\EntityManager::clear()
使用$entityName
?的EntityManager:只有某些类型的
比方说,我有一个Post
实体与Author
和Category
。我想添加帖子到我的数据库(随机,从CSV文件或任何)。我在添加数据时遇到性能问题。内存使用增加,并减慢了进程,但我不知道如何(部分)清除EntityManager。
这就是魔法发生循环:
$batchSize = 250;
foreach ($posts as $post) {
//$post['author'] is instance of Acme\BlogBundle\Author
$post = new Post($post['author'], $post['category']);
$em->persist($post);
if ($i % $batchSize == 0) {
$entity = '??';
$em->flush();
$em->clear($entityName);
gc_collect_cycles();
}
}
这将引发Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship Acme\BlogBundle\Post#author
。这是正确的,因为Author
是由已被清除EntityManager管理的。
这是明确的方法:
namespace Doctrine\ORM;
class EntityManager implements EntityManagerInterface {
/**
* Clears the EntityManager. All entities that are currently managed
* by this EntityManager become detached.
*
* @param string|null $entityName if given, only entities of this type will get detached
*
* @return void
*/
public function clear($entityName = null)
{
$this->unitOfWork->clear($entityName);
}
}
因为我要清除的帖子,但要保留作者和类别实体类型,我预计$entityName = 'Acme\BlogBundle\Entity\Post'
,$entityName = get_class($post);
或$entityName = Acme\BlogBundle\Entity\Post::class
会有所帮助,但不幸的是它不清除任何东西。
如何正确使用这个参数?
我猜你必须通过某事类同'“AcmeBlogBundle:邮报”' –
可能的重复http://stackoverflow.com/questions/19061870/doctrine-entitymanager-clear-doesnt-fully-clear? –
@johnSmith不,没有任何区别。 –