Query Builder: implicit double quotes in ->orderBy();


if I want to use

->orderBy('FIELD(my_field.id,' . implode(',', $my_array_with_ids) .')')

with the yii 2 query builder, the positions of the double quotes aren’t right in the final sql query:

ORDER BY FIELD(my_field.id, `"1634`, `1641`, `1637`, `1638`, `1639`, `1640`, `1642`, `1644`, `1647`, `809329")`

A mysql error is produced. Is there any “yii 2 way” to use the mysql Field(); function and prevent yii from setting the quotes? Or is there any different way to pass my arguments to orderBy(); and beeing sure that the sql query isn’t causing an error?

Any help would be appreciated.

Thank you.



I think you need to use a db Expression eg,

->orderBy(new Expression(‘FIELD(my_field.id,’ . implode(’,’, $my_array_with_ids) .’)’)