dongtangze6393 2014-09-25 15:22
浏览 37
已采纳

如何在刷新实体管理器之前关联foreach循环中的多个Doctrine对象

i need your help for persist data, i explain : i have entity Player with variable refer Team :

class DataPlayer
{
    /**
     * @ORM\ManyToOne(targetEntity="Team")
     * @ORM\JoinColumn(name="tag_team", referencedColumnName="tag_team")
     */
    private $team;
    ...
}

but when i include the data, i have only tag of team, not entity team... because the team is not probably present in database (i include team after). how can i do to set team with string (the tag directly) whitout to change Entity player

thank!

  • 写回答

1条回答 默认 最新

  • doumei3828 2014-09-25 18:24
    关注

    Okay, I believe I see what you're trying to achieve.

    You are doing some sort of import, and during the foreach loop, you're creating Team entities that get associated with your DataPlayer. Obviously you don't want to end up making multiple Teams after the first one has been made with a certain tag, but since you haven't performed a flush() using the Entity Manager yet, you can't findOneByTag() because the Team doesn't yet exist in the database.

    This is obviously problematic. So what's the solution? Create a temporary array!

    $tempTeams = array();
    foreach($teams as $team){
        $info = explode(',', str_replace("'", "", $team));
        if (isset($tempTeams[$info[1]])) {
            $db_team = $tempTeams[$info[1]];
        } else {
            $db_team = $db->getRepository("ApplicationTestBundle:Team")->findOneByTag($info[1]);
        }
        if(!$db_team){
            $db_team = new Team();
            $db_team->setTag($info[1]);
            $db_team->setName($info[0]);
            $em->persist($db_team);
            $tempTeams[$info[1]] = $db_team;
        }
        $dataT = new DataTeam();
        $dataT->setTeam($db_team);
        $em->persist($dataT);
        $db_team = false; // Need to make sure $db_team is cleared out for the next iteration of the foreach
    }
    

    This takes all of your temporary PHP objects before persistence and buffers it into the temporary array, which allows you to recall your new Objects by tag name with no problems. The other solution is to $em->flush(); after $em->persist($db_team);

    You can then access the $tempTeams array for later lookups (I believe there was an issue with using $info[4] for the tag this time):

    foreach($players as $player){
        $info = explode(',', str_replace("'", "", $player));
        $db_player = $db->getRepository("ApplicationTestBundle:Player")->findOneByPseudo($info[1]);
        $dataJ = new DataPlayer();
        $dataJ->setJoueur($db_player);
        if (isset($tempTeams[$info[4]])) {
            $db_team = $tempTeams[$info[4]];
        } else {
            $db_team = $db->getRepository("ApplicationTestBundle:Team")->findOneByTag($info[4]);
        }
        $dataJ->setTeam($db_team);  
        $em->persist($dataJ);
        $db_team = false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试