Help with ActiveDataProvider sorting

Hi -

I have a very simple case of a Gii generated crud grid (in index.php). It is for a log database table that I would like to view the default ‘id’ column in Descending order. I can’t seem to make it work, this is what I have. The column is ‘id’ that I’m trying to change is a database col not the yii generated counter col.

Not sure what I’m missing but likely something simple ;)

Sandy




	public function search($params)

	{

    	$query = Logger::find();


    	$dataProvider = new ActiveDataProvider([

        	'query' => $query,

    	]);


    	//

    	// I added sort descending on id col

    	//


   	$dataProvider->setSort([

   			'attributes' => [

   			'id' => ['default' => SORT_DESC],

			]

		]);


   	//

   	// end of added code

   	//


    	$this->load($params);


    	if (!$this->validate()) {

        	// uncomment the following line if you do not want to any records when validation fails

        	// $query->where('0=1');

        	return $dataProvider;

    	}


    	$query->andFilterWhere([

        	'id' => $this->id,

        	'ed_status' => $this->ed_status,

        	'dte_update' => $this->dte_update,

    	]);


    	$query->andFilterWhere(['like', 'data_name', $this->data_name])

        	->andFilterWhere(['like', 'ed_msg', $this->ed_msg])

        	->andFilterWhere(['like', 'ed_url', $this->ed_url]);


    	return $dataProvider;

	}



One more interesting thing is that it seems that in using the code snip to mess with the default sort it seems you must specify any fields that are to also be allowable to sort. So in my example I can now only manually sort the ‘ID’ field, all other fields in the grid are now not sortable. So I’m guessing the idea is to include all fields that can be sorted.

Just not sure how to set the initial sort to DESC.

Sandy

By calling this method you are not adding the default. You are overriding the whole Sort config.

I suppose you would like to keep the initial config for all attributes and additionally set the default sorting.

You can see the example below of how it can be done. I’m including the rest of the search method for the context (there is also some extra stuff you can ignore).


public function search($params)

{

    $query = Order::find();


    $dataProvider = new ActiveDataProvider([

        'query' => $query,

    ]);


    $query->joinWith(['clientPerson', 'orderType', 'product']);


    $dataProvider->sort->attributes = array_merge($dataProvider->sort->attributes, [

        'clientPerson.fullName' => [

            'asc' => ["CONCAT([[client_person]].[[last_name]], ' ', [[client_person]].[[first_name]])" => SORT_ASC],

            'desc' => ["CONCAT([[client_person]].[[last_name]], ' ', [[client_person]].[[first_name]])" => SORT_DESC],

        ],

        'order_type_id' => [

            'asc' => ['order_type.name' => SORT_ASC],

            'desc' => ['order_type.name' => SORT_DESC],

        ],

        'product.name' => [

            'asc' => ['product.name' => SORT_ASC],

            'desc' => ['product.name' => SORT_DESC],

        ],

    ]);


    $dataProvider->sort->defaultOrder = ['receipt_date' => SORT_DESC];


    if (!($this->load($params) && $this->validate())) {

        return $dataProvider;

    }


    $query->andFilterWhere([

        'id' => $this->id,

        'receipt_date' => $this->receipt_date,

        'order_type_id' => $this->order_type_id,

    ]);


    $query->andFilterWhere(['like', "CONCAT([[client_person]].[[last_name]], ' ', [[client_person]].[[first_name]])", $this->getAttribute('clientPerson.fullName')])

            ->andFilterWhere(['like', 'product.name', $this->getAttribute('product.name')]);


    return $dataProvider;

}

Vojtech -

Thanks for the quick help that did the trick. Yes I did notice I lost the ability to sort on any fields after messing with the code I had. It basically set the only allowable field to ‘Id’ which was not the desired result. I always expect that Yii’s solution is very simple, and in this case it was too.

Again thanks for the help and ‘knowledge’ transfer!

Sandy