Good day!
I have a quick question.
How do we input variables into the WHERE parameters portion of the code below
$ids=“4,8,15”;//i’ve tried many variations of this
$query = $searchmodel::find()->where([‘id’ => [4, 8, 15]]);
$query = $searchmodel::find()->where([‘id’ => [$ids]]);
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!
Bizley
(Bizley)
May 27, 2016, 6:16am
2
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)’.
Bizley:
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
$query = $searchmodel::find()->where([‘id’ => [1, 2, 3,4,5,6,7,8]]);
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.
Bizley
(Bizley)
May 27, 2016, 6:33am
4
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!