简体   繁体   中英

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

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)"

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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