Doctrine 2 Bulk insert with relation

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 13:01:47

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/21931067/doctrine-2-bulk-insert-with-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!