主义2散装插入与关系 [英] Doctrine 2 Bulk insert with relation

查看:126
本文介绍了主义2散装插入与关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的数据库中做一些批量插入。我已经阅读了关于教义方面的文章,并且想要使用零星刷新和清除,以防止高内存消耗。不幸的是,所有的实体在这个过程中脱身,不仅是我插入的,而且与它的关系。



我试图重新合并或使用引用。在我目前的情况下,我使用的是引用,但是我仍然收到以下错误消息:


[Doctrine\ORM\ORMInvalidArgumentException] br>
通过关系
'Strego\TippBundle\Entity\Bet#betRound'找到一个新实体,该对象未配置为
实体的级联持久化操作:LoadTest GameGroup。要
解决这个问题:或者显式地调用
上的EntityManager#persist()这个未知实体或配置级联在
中保留这个关联,例如@ManyToOne(..,cascade = {坚持})。


相关编码是这样的:

  // BetRound 
print(PHP_EOL。Search for BetROund);
$ betRounds = $ em-> getRepository('StregoTippBundle:BetRound') - > findAll();
print(PHP_EOL.'found Betrrounds:'。count($ betRounds));
$ betRound = current($ betRounds);
...
//参考资料
$ betRoundRef = $ em-> getReference('Strego\\TippBundle\\\Entity\\BetRound',$ betRound- >的getId());

这里插入:

  foreach($ gameRefs as $ game){
$ bet = new GameBet();
$ bet-> setBetround($ betRoundRef);
$ bet-> setUser($ genuser);
$ bet-> setGame($ game);
$ bet-> setScoreT1($ this-> getScore());
$ bet-> setScoreT2($ this-> getScore());
$ bet-> recalculatePoints();
$ em-> persist($ bet);
}

if(($ i%self :: $ batchSize)== 0){
$ em-> persist($ userGroup);
$ em-> persist($ mySelf);
$ em-> flush();
$ em-> clear();

$ em-> merge($ betRound);
$ em-> merge($ userGroup);
$ em-> merge($ mySelf);
}

我的整个Fixture加载这个数据可以在这里找到: https://gist.github.com/KeKs0r/a3006768db267311bb35

解决方案

调用清除方法时,所有内容都会分离(分离实体)。
您需要重新加载每个先前加载的实体(在您的情况下,您可以使用$ betRoundRef,$ genuser和大概$游戏)。



a href =https://stackoverflow.com/questions/7431794/doctrine-2-weird-behavior-while-batch-processing-inserts-of-entities-that-refer>此堆栈溢出回答


I am trying to do some Bulk inserts into my database. I have read the article about it on the doctrine side and wanted to use sporadic flushs and clears in order to prevent high memory consumptions. Unfortunately all entities get detached in the process, not only the ones I am inserting, but also the relations to it.

I tried to remerge them or use references instead. In my current case I am using a reference and still I get the following error message:

[Doctrine\ORM\ORMInvalidArgumentException]
A new entity was found through the relationship 'Strego\TippBundle\Entity\Bet#betRound' that was not configured to cascade persist operations for entity: LoadTest GameGroup. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).

The relevant coding is this:

    // BetRound
    print(PHP_EOL."Search for BetROund");
    $betRounds = $em->getRepository('StregoTippBundle:BetRound')->findAll();
    print(PHP_EOL.'found Betrrounds:'.count($betRounds));
    $betRound = current($betRounds);
...
// References
        $betRoundRef = $em->getReference('Strego\\TippBundle\\Entity\\BetRound', $betRound->getId());

and here the insert:

    foreach($gameRefs as $game){
        $bet = new GameBet();
        $bet->setBetround($betRoundRef);
        $bet->setUser($genuser);
        $bet->setGame($game);
        $bet->setScoreT1($this->getScore());
        $bet->setScoreT2($this->getScore());
        $bet->recalculatePoints();
        $em->persist($bet);
    }

    if(($i % self::$batchSize) == 0){
        $em->persist($userGroup);
        $em->persist($mySelf); 
        $em->flush();
        $em->clear();  

        $em->merge($betRound);
        $em->merge($userGroup);
        $em->merge($mySelf);
    }

My whole Fixture for loading this data can be found here: https://gist.github.com/KeKs0r/a3006768db267311bb35

解决方案

When calling the clear method everything is detached (Detaching entities). You'll need to reload each previously loaded entity (in your case $betRoundRef, $genuser and probably $game too).

Have a look at this Stack Overflow answer

这篇关于主义2散装插入与关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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