我怎样才能使用" INSERT SELECT"在symfony3中?

时间:2017-01-16 22:38:22

标签: php mysql doctrine-orm symfony

我想构建查询,我将数据插入mysql。

 $data=$request->request->all();
     $odd=$data['form']['odd'];
     $stake=$data['form']['stake'];
     $win=$data['form']['win'];
     $login_user=$data['form']['login_user'];

    $separator = '\r\n';

    $db = $this->getDoctrine()->getManager();
    $query = "INSERT INTO zakladyuser (game, type, odd, stake, win, login_user, stat)
              SELECT GROUP_CONCAT(game SEPARATOR :separator ), GROUP_CONCAT(type SEPARATOR :separator ), :odd, :stake, :win, :login_user, 0
              FROM kupon k0";
    $stmt = $db->prepare($query);
    $params = array(
                    "separator"=>$separator,
                    "odd"=>$odd,
                    "stake"=>$stake,
                    "win"=>$win,
                    "login_user"=>$login_user
                  );
    $stmt->execute($params);
    $results = $stmt->fetchAll();

我能纠正什么?因为我有这些错误:

  

调用未定义的方法Doctrine \ ORM \ EntityManager :: prepare()

     

尝试调用名为&#34的未定义方法;准备" class" Doctrine \ ORM \ EntityManager"。

如果这种方式出错,我该怎样做得更好?

1 个答案:

答案 0 :(得分:1)

您需要在prepare()对象中调用connection

$db = $this->getDoctrine()->getManager();
..
$stmt = $db->getConnection()->prepare($query);