Default order in actionAdmin

I have a model called Expense that I want to set the default order when viewing in expense/admin. I can see that this question has been asked many times before but I don’t think the responses are compatible with Yii 1.1.2.

If I want to set the default order do I do this in actionAdmin() or elsewhere in the controller?

By default, the code currently looks like the following:


    public function actionAdmin()

    {

        $model=new Expense('search');

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

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


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

            'model'=>$model,

        ));

    }

Usually, I will put


$criteria->order = "some_field ASC";

in search() function in the model file (might be model/Expense.php in your case)

Thanks junxiong,

That answered my question. I also found that if I use defaultOrder() rather than order() it still allows you to re-order by other columns if need be.

I tried applying the same to another model that is including a field from another table but this seems to ignore order when I use defaultOrder(). However it works as expected when I use order(). I’m a bit lost on this one.




		$criteria=new CDbCriteria;


		// Force model to only show invoices for current business

		$criteria->alias = 'Invoice';

		$criteria->select = 'Invoice.*';

		$criteria->join='LEFT JOIN Client ON Client.id=Invoice.clientId';


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


	        .......


		$criteria->defaultOrder = "id DESC";


		$criteria->condition='Client.businessId='. Yii::app()->userInfo->business;

			return new CActiveDataProvider('Invoice', array(

			'criteria'=>$criteria,

		));




Can anyone suggest why order() works here but not defaultOrder() ?

cuz defaultOrder is not a property of criteria, it’s a property of csort. you should turn on error reporting

Hello, my friend. If you chose simple $criteria->order = "field ORDERTYPE", the sortable will not work fine. Just do this: in search() method use return new CActiveDataProvider($this, array(

		'criteria'=>$criteria,           


                    'sort'=>array('defaultOrder'=>'value ASC or DESC')


	));

And sortable will work fine. Think shaked.