1

I want to build a query using where with multiple values. Number of values is variable (from one to ten). My query looks like this for now:

return $repository->createQueryBuilder('s')
    ->where('s.id = :id')
    ->setParameter('id', '1')

But I want that id parameter to be array. I tried setParameters, but that's not the function.

1 Answer 1

2

Try this

return $repository->createQueryBuilder('s')
   ->where('s.id IN (:id)')
   ->setParameter('id', array('1','2','3'))

This uses the array passed as a parameter to be using in the IN statment.

To use a variable do this :

$myarray = array('1','2','3');
return $repository->createQueryBuilder('s')
   ->where('s.id IN (:id)')
   ->setParameter('id', $myarray)
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, works great! Can you tell me also, how can I pass an variable instead of array? When I define $asd variable outside the builder, and then I want to use it, Symfony returns unknown variable.
Yeah, and I was in the form builder :) So I had to use use. But nevertheless, your answer helped me A LOT. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.