简体   繁体   中英

Symfony2 doctrine mysql IN query

I have array of products IDs. I have to make query like this:

SELECT * FROM products WHERE pid IN (1, 2, 8, 4, ...) // etc

I have my ids in variable $pids.

$qb = $em->createQueryBuilder();
$query = $qb->select('p.pid')
            ->from('SRC\MainBundle\Entity\Product', 'p')
            ->where('p.name IN :pids') // error is HERE
            ->setParameter('pids', $pids)
            ->getQuery();

Doesn't work. I get an error:

[Syntax Error] line 0, col 66: Error: Expected Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS, got ':pids'

What if you try

->where('p.name IN (:pids)') // error is HERE

It explicitly says to you that it expects parentheses but gets placeholder

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