Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2: How to add two fields in orderby() of Find()

Tags:

php

yii2

How to add more than one field to sort in find() method?

I have tried as below

$model::find()->orderBy([['id_date' => SORT_DESC],['item_no'=>SORT_ASC]);

But it is throwing error with query. Orderby Query produced by yii2 is: ORDER BY 0, 1

like image 932
Kumar V Avatar asked Jun 15 '16 08:06

Kumar V


2 Answers

According to the documentation:

$model::find()->orderBy([
  'id_date' => SORT_DESC,
  'item_no'=>SORT_ASC
]);
like image 88
Jurik Avatar answered Oct 19 '22 13:10

Jurik


You have a syntax error in the following code:

$model::find()->orderBy([['id_date' => SORT_DESC], ['item_no' => SORT_ASC]);

The correct way of doing this is:

$model::find()->orderBy(['id_date' => SORT_DESC, 'item_no' => SORT_ASC]);
like image 26
Gullu Mutullu Avatar answered Oct 19 '22 12:10

Gullu Mutullu