Default model items sorting

Hi,

In my code


public function actionAdmin()

    {

        $model=new ItAdsFields('search');

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

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

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


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

            'model'=>$model,

        ));

    }

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?

thanks

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(

	    'criteria'=>$criteria,

        'sort'=>array(

          'defaultOrder'=>'id DESC',

        ),

        'pagination'=>array('pageSize'=>25),

      ));

ah! hadn’t noticed the ordering had stopped!

Thanks for the tip.