How To Append Sort Order

In a grid view users can sort the result by any column. I want to append a sort order to the selected sort column as a second sort argument.


$query->addOrderBy('myDefaultSortColumn');

The above statement in a search model executes before Yii creates the order statement based on the clicked grid column.

How can I add some (default) column(s) to the sort order?

this should do it


<?php


$dataProvider = new ActiveDataProvider([

     'query' => $query,

     'sort'=> ['defaultOrder' => ['columnName'=>SORT_ASC]]

 ]);

Unfortunately not. The defaultOrder is only used for the initial sort order. Once a user clicks in a grid header only this column (exactly the sort attribute) will be taken to generate the order by clause.

Is there an other possible solution or should the defaultOrder work as you mentioned and it’s a bug?

Something like this works for me (sorting a relation attribute ‘section_name’):




    $dataProvider->setSort([

        'attributes' => [

            'id',

            'sort',

            'section_name' => [

                'asc' => ['section_lang.section_name' => SORT_ASC],

                'desc' => ['section_lang.section_name' => SORT_DESC],

                'label' => 'Section Name'

            ]

        ]