How to implement sorting on DATE/TIME column in CGridView by using CDbCriteria Yii

I am struck in a Problem from last three day but didn’t find any useful solution

I want to implement a custom CDbCriteria on CGridView to sort the LD_DATE Column in Desending order. But each time it sort the Column as String.

I want to sort on the base on Data and Time. Please any body can figure out the issue. What the wrong with my code

LogDetailController.php


public function actionAdmin($id){       

        $criteria=new CDbCriteria;

        $criteria->select=" LD_ID, LM_ID, LD_TITLE, LD_DESC, LD_CONTROLLER, LD_ACTION, LD_ACTION_ID, LD_DATE ";

        $criteria->order = "LD_DATE DESC";

        $criteria->limit = "25";

        $criteria->addCondition("LM_ID=:LM_ID");

        $criteria->params=array(':LM_ID'=>$id);

        $dataProvider = LogDetail::model()->findAll($criteria);

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

                'model'=>$dataProvider,

        ));

}

admin.php


$this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'log-detail-grid',

    'dataProvider'=>$model,

    'columns'=>array(

        'LD_TITLE',

        'LD_DESC',

        'LD_CONTROLLER',

        'LD_ACTION',

        'LD_ACTION_ID',

        'LD_DATE',

    ),

)); 

Database Script


CREATE TABLE `log_detail` (

  `LD_ID` int(12) unsigned NOT NULL AUTO_INCREMENT,

  `LM_ID` int(10) unsigned NOT NULL,

  `LD_TITLE` varchar(100) DEFAULT NULL,

  `LD_DESC` varchar(200) DEFAULT NULL,

  `LD_CONTROLLER` varchar(50) DEFAULT NULL,

  `LD_ACTION` varchar(50) DEFAULT NULL,

  `LD_ACTION_ID` int(11) DEFAULT NULL,

  `LD_DATE` datetime DEFAULT NULL,

  PRIMARY KEY (`LD_ID`),

  KEY `LM_ID` (`LM_ID`),

  CONSTRAINT `log_detail_ibfk_1` FOREIGN KEY (`LM_ID`) REFERENCES `log_master` (`LM_ID`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

From this code i am getting this error


Fatal error: Call to a member function getData() on a non-object in *************************************\protected\framework\zii\widgets\CBaseListView.php on line 111

What I want ????

I want to display all record in DESENDING ORDER BY DATA AND TIME

Hi Uffan

Try adding the tbl alias (t.):


$criteria->order = "t.LD_DATE DESC";

Check link

You should pass an actual dataprovider to the CGridView, not an array of models.