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 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi