Yii: Reverse CGridView display order

How can I reverse the order of CGridView (showing newest on top by default)?

I currently have the Gii generated code:


public function actionAdmin() {

    $model = new Post('search');

    $model->unsetAttributes();

    if (isset($_GET['Post']))

        $model->attributes = $_GET['Post'];


    $this->render('admin', array(

        'model' => $model,

    ));

Please help. Thanks.

Modify the search() method in your Post model and set the default order of the sort.




	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('foo',$this->foo,true);

		$criteria->compare('bar',$this->bar,true);

		....


		return new CActiveDataProvider(get_class($this), array(

			'criteria' => $criteria,

+			'sort' => array(

+			 	'defaultOrder' => 'post_date DESC',  // this is it.

+			),

			'pagination' => array(

				'pageSize' => 30,

			),

		));

	}



In the above, the ‘pageSize’(rows per page of a grid) is also defined. It is not included in gii-genarated code, but it’s worth noting that you can set it at this point.

Thanks for the tip softark. I was doing it differently, modifying the order attribute like this:


$criteria->order = 'post_date DESC';

It works, but breaks the sorting capability of the grid. Your code works perfectly