How to make multiple WHERE IN column query in Doctrine query builder?

白昼怎懂夜的黑 提交于 2019-12-05 00:44:23

问题


I would like to update multiple records in db using WHERE IN statement with two column check.

Pure MySql raw query looks something like this.. and it works:

UPDATE poll_quota q SET q.count = q.count+1 WHERE q.form_id=14 AND ((q.field_id,q.value) IN (('A',1),('B',1)))

My code:

$this->createQueryBuilder("q")
            ->update()
            ->set("q.count","q.count+1")
            ->where("q.form_id=:form_id")
            ->andWhere("((q.field_id,q.value) IN (:wherein))")
            ->setParameter(":form_id",$form_id)
            ->setParameter(":wherein",$where_in)
            ->getQuery()
            ->execute()
        ;

Output:

[Syntax Error] line 0, col 103: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','

[1/2] QueryException: UPDATE Edge\PollBundle\Entity\Quota q SET q.count = q.count+1 WHERE q.form_id=:form_id AND ((q.field_id,q.value) IN (:wherein))   +

Attemp to do sth like this, also doesn't work:

[...]->andWhere("((q.field_id,q.value) IN ({$where_in}))")

$where_in contains string like this:

"('A',1),('B',1)"

回答1:


DQL doesn't allow using multiple columns in a WHERE IN statement since not all DBMS support it. You can run it with the raw SQL using $this->getEntityManager()->getConnection()->executeUpdate()



来源:https://stackoverflow.com/questions/31435177/how-to-make-multiple-where-in-column-query-in-doctrine-query-builder

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