defaultOrder String

Hi, I need use defaultOrder in my model with ActiveDataProvider, but I can´t write string.

This run correct:


 

$dataProvider->setSort([

         	'defaultOrder' => ['id'=>SORT_DESC]

            ]); 



but I need sort by a string default,

this not work:




        $dataProvider->setSort([

         	'defaultOrder' => 'ABS( DATEDIFF( start_date, NOW() ) ) '

            ]); 



Sort object is not db specific. It can be used with ArrayDataProvider. So it’s probably impossible to use this kind of db expression in it.

But I think you can select that expression as a field and use it for sorting.




$query->select(['*', 'ABS( DATEDIFF( start_date, NOW() ) ) as dateDiff']) ...


$dataProvider->setSort([

    'defaultOrder' => ['dateDiff' => SORT_ASC]

]);



Check also the following section of the guide.

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#selecting-extra-fields

Wou thank you, this work fine.

also I had thought that if I get the gridview order (the user check in a field for order), I use orderBy() and this work too.

For example:




        if ($CheckOrder) {

            $query->orderBy('ABS( DATEDIFF( start_date, NOW() ) )');

        }



[color="#212121"][font="arial, sans-serif"] [/font][/color]