Answer the question
In order to leave comments, you need to log in
How to pull out the ID in AR in the order I need?
There is a list of IDs in a certain order, for example 2, 3, 1, 5,
I need to get records from the table in this order, I do Table::find() ->where(['id' =>$ids])->all();
it but I get records in ascending order, i.e. 1, 2, 3, 5
and I need 2.3, 1,5
in query log
WHERE `id` IN ('2', '3', '1', '5')
Answer the question
In order to leave comments, you need to log in
$items = Table::find()->where(['id' =>[1, 2, 3, 5]])->indexBy('id')->all();
$items = array_replace([2 => null, 3 => null, 1 => null, 5 => null], $items);
You need to add sorting by the field, according to which the records will line up in the desired order:
By specifying the sort order SORT_ASC or SORT_DESC
By default, sorting is by the primary key, the sort order is "ascending"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question