Cgridview default sort order by primary key desc globally

I am want to sort gridview data by default order by primary key Desc globally so how can i do that.

I know this way but how to do it globally for all model


return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

                        'sort'=>array(

                            'defaultOrder'=>'project_id DESC',

                         ),

		));

Create a base class for all models that will extend this and put your code in a method of base class,

to be called from all models.

i have done like this but it is not working,


public function defaultScope() {

        return array(

            'sort'=>array(

                            'defaultOrder'=>$this->primaryKey.' DESC',

                         ),

        );

    }

This works for me, but it gives error


 

public function defaultScope() {

        return array(

            'order' => "t.".$this->tableSchema->primaryKey.' DESC',

        );

    }



Currenlty I am in TaskController and view project name in cdetailview and gives below error.


Column not found: 1054 Unknown column 't.project_id' in 'order clause'. The SQL statement executed was: SELECT `project`.`project_id` AS `t1_c0`, `project`.`project_name` AS `t1_c1`, `project`.`completed` AS `t1_c2`, `project`.`due_date` AS `t1_c3`, `project`.`created` AS `t1_c4`, `project`.`updated` AS `t1_c5` FROM `project` `project` WHERE (`project`.`project_id`=:ypl0) ORDER BY t.project_id DESC

Hi Yatin,

from the SQL it looks like mysql is not recognizing the column, it’s using full table.column naming in the error so try:


return new CActiveDataProvider($this, array(

                        'criteria'=>$criteria,

                        'sort'=>array(

                            'defaultOrder'=>'project.project_id DESC',

                         ),

                ));

Yes. I know about this. But i want it globally for all model. So i placed my code in base model


defaultScope()

but in relational model it is not working on Cdetailview.