Default model items sorting


In my code

public function actionAdmin()


        $model=new ItAdsFields('search');

        $model->unsetAttributes();  // clear any default values







I wannt to add default sorting option, for example ‘sort’ => ‘position ASC’

But I don’t know how, because in this way by default controller generates this code, but if I change it to dataprovider, it doesnt works, because of some missing functions.

How can I add sorting option in this code example?


you can do it in defaultScope function in your model class

is it not ‘order’ that you are looking for? ie…

$dataProvider=new CActiveDataProvider('yourModel', array(

		'criteria' => array('condition' => 'yourColumn = '.yourValue, 'order'=>'columnToSortBy DESC')));

You probably don’t want to use ORDER if you are going to be allowing your users to change the ordering in a datagrid. ;D

You’ll want to change defaultOrder:

	  return new CActiveDataProvider('inspection', array(



          'defaultOrder'=>'id DESC',




ah! hadn’t noticed the ordering had stopped!

Thanks for the tip.