Sorting CGridView results

Hello, I want to sort my CGridView results by name (not id). Unfortunately, when I change the model’s search function to include




$criteria->order = "name DESC";



when I try to sort the grid by clicking the name attribute, it doesn’t do anything and I can only sort the other attributes. Makes sense since it keeps returning the same dataprovider with that name sort. Anyways, I was just wondering what the cleanest/easiest way is to be able to sort all columns in the grid while still having the initial sort be by name instead of id? Thanks.

Did you try this:




    $dataProvider->sort->defaultOrder = "name DESC";



As maschingan, but defined in the search method within the model (I believe the data provider adds the sort upon your already defined criteria, so it doesn’t overwrite it):


public function search()

{

	$criteria = new CDbCriteria;

	

	...

	

	return new CActiveDataProvider('YourModel', array(

		'criteria' => $criteria,

		'sort' => array(

			'defaultOrder' => 'name DESC',

		),

	));

}

Doh! The examples I looked at didn’t mention defaultOrder…thanks guys works like a charm!