[Active Query] Variables in Where

Good day!

I have a quick question.

How do we input variables into the WHERE parameters portion of the code below

This same issue will arise if i want to put variables in the "options" of gridView or other widgets.

I’ve read through this http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html and could not quite find what I am looking for.

If using a variable like $ids does not work, how should i implement the dynamic queries where i have a varied amount of parameters e.g. a range of 1-100 different ids etc?

Thank you!

What is exactly what you want to achieve?

The second line in your quote is correct - you can pass array there to get ‘WHERE id IN (4, 8, 15)’.

Thank you for your response!

Essentially, I have a list of user ids, which may vary depending on what is being searched. And I am displaying this in a listView.

I am putting the list into the variable $ids, so that i can dynamically display lists of different lengths in my listView.

For example in a given instance it must execute this:

and in another, this

for example.

I dont know how many parameters(ids) there will be, and I dont know which ids they will be, so i need the queries to be dynamic in that sense.

In this case as long as $ids is single integer or array of integers you can use


$query = $searchmodel::find()->where(['id' => $ids]);

Hi,

THanks! I will give the array a shot!