简体   繁体   English

使用IN Clause进行Symfony2.3原始sql查询

[英]Symfony2.3 raw sql query with IN Clause

I was trying to a run raw sql query with doctrine entitymanager for IN clause as shown below. 我正在尝试使用doctrine entitymanager为IN子句运行原始sql查询,如下所示。

    $idSArray  = Array ( [0] => 1 [1] => 2 )

    $stmt = $this->getDoctrine()->getEntityManager()
    ->getConnection()
    ->prepare('SELECT t1.id , t1.name , t2.start_date , t2.end_date 
    FROM table1 t1 , table2 t2 
    WHERE t1.id = t2.matchId AND  t1.id IN (:ids)');


    $params = array(
      'ids'  => implode( ",", $idSArray )
    );
    $stmt->execute($params);
    $results = $stmt->fetchAll();

But I am only getting result for Id = 1. If I hardcode the WHERE IN condition as 但我只得到Id = 1的结果。如果我将WHERE IN条件硬编码为

     WHERE t1.id = t2.matchId AND  t1.id IN (1,2)');

Then getting result for both the Ids. 然后获得两个ID的结果。 Can anyone tell me that what I am doing wrong in passing the $params array. 谁能告诉我在传递$ params数组时我做错了什么。 I have also print the implode result which outputs 1,2. 我还打印了输出1,2的内爆结果。 So I am not able to find the mistake and also the way to perform raw sql query with IN clause. 所以我无法找到错误以及使用IN子句执行原始sql查询的方法。

Answer: 回答:

So there are at least two mistakes you did. 所以你做的至少有两个错误。 The first is what @Alarid said: you should not implode your array. 第一个是@Alarid所说的:你不应该破坏你的阵列。 The second is that you have to use DoctrineDBALTypes Conversion for IN clause when running a prepared statement. 第二个是在运行预准备语句时必须使用DoctrineDBALTypes Conversion for IN clause

And finally your query goes this: 最后你的查询是这样的:

$stmt = $this->getDoctrine()->getEntityManager()
        ->getConnection()
        ->prepare('SELECT t1.id , t1.name , t2.start_date , t2.end_date
        FROM table1 t1 , table2 t2
        WHERE t1.id = t2.matchId AND  t1.id IN (:ids)');

$stmt->bindValue('ids', $idSArray, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
$stmt->execute();

Or alternative: 或替代方案:

$stmt = $this->getDoctrine()->getEntityManager()
    ->getConnection()
    ->executeQuery('SELECT t1.id , t1.name , t2.start_date , t2.end_date
        FROM table1 t1 , table2 t2
        WHERE t1.id = t2.matchId AND  t1.id IN (:ids)',
        array('ids' => $idSArray),
        array('ids' => \Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
    )
;

You have to let your array as an array, don't implode it. 你必须让你的数组作为一个数组,不要破坏它。

$params = array(
   'ids'  => $idSArray
);

@Hast The second code block you posted works. @Hast您发布的第二个代码块有效。 Adding a new answer so future viewers can reference... 添加新答案,以便将来的观众可以参考......

$ids = [1,2];

$sql = "SELECT t1.id , t1.name , t2.start_date , t2.end_date
        FROM table1 t1 , table2 t2
        WHERE t1.id = t2.matchId AND  t1.id IN (?)";

$stmt = $this->getEntityManager()->getConnection()->executeQuery(
        $sql,
        [$ids],
        [\Doctrine\DBAL\Connection::PARAM_INT_ARRAY] // for an array of strings use PARAM_STR_ARRAY
    );

$data = $stmt->fetchAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM