如何使用 CDbCriteria Yii 对 CGridView 中的 DATE/TIME 列进行排序

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

我在过去三天遇到了一个问题,但没有找到任何有用的解决方案

我想在 CGridView 上实现自定义 CDbCriteria 以按降序对 LD_DATE 列进行排序。但是每次它将列排序为字符串。

我想根据数据和时间进行排序。请任何机构都能弄清楚这个问题。我的代码有什么问题

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',
    ),
)); 

数据库脚本

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

试试这个:

$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',
    array(
      'name'=>'LD_DATE',
      'header'=>'LD_DATE',
      'value'=>function ($data, $row){
          echo $data->LD_DATE
       },
      'type'=>'date',
    ),
  ),
));

如果不起作用,请尝试 'type'=>'datetime''type'=>'dateTime'。希望对您有所帮助。

我找到了解决方案并且工作正常。

LogDetail.php

class LogDetail extends CActiveRecord {
    public function viewLog($id) {
        $criteria=new CDbCriteria;
        $criteria->alias="t";
        $criteria->select="t.LD_ID, t.LD_TITLE, t.LD_DESC, t.LD_DATE, t.LD_CONTROLLER, t.LD_ACTION, t.LD_ACTION_ID";
        $criteria->limit = "50";
        $criteria->addCondition("LM_ID=:LM_ID");
        $criteria->params=array(':LM_ID'=>$id);
        $criteria->order = " LD_DATE DESC ";
        return new CActiveDataProvider($this,array('criteria'=>$criteria,'pagination' => array('pageSize' => 50)));
    }
}

LogDetailController.php

class LogDetailController extends Controller {
    public function actionHistory($id) {
        $model = new LogDetail('viewLog');
        $model = $model->viewLog($id);

        $this->render('logdetail',array(
                'dataProvider'=>$model, 
        ));
    }
}

logdetail.php

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'log-detail-grid',
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        array(
            'name' => 'LD_TITLE',
            'type' => 'raw',
            'value' => '$data->LD_TITLE'
        ),
        array(
            'name' => 'LD_DESC',
            'type' => 'html',
            'value' => '$data->LD_DESC."<br />Action: <strong><u>".Yii::app()->request->baseUrl."/index.php/".$data->LD_CONTROLLER."/".$data->LD_ACTION.(($data->LD_ACTION_ID != null) ? ("/".$data->LD_ACTION_ID) : ("") )."</u></strong><br />"'
        ),
        array(
            'name' => 'LD_DATE',
            'type' => 'html',
            'value' => '"<center>Date:&nbsp;".explode(" ", $data->LD_DATE)[0]."<br />Time:&nbsp;".explode(" ", $data->LD_DATE)[1]."</center>"'
            ),
        ),
    )
);