0

您好,我需要按 id DESC 对 cgridview 数据表进行排序,我该怎么做?

我的错误代码:

public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('ID',$this->ID);
        $criteria->compare('name',$this->name,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>array(
                    'ID'=>array(
                        'desc'=>'ID DESC',
                    ),
            ),
        ));
    }
4

4 回答 4

3

尝试更接近:

$criteria=new CDbCriteria;

$criteria->compare('ID',$this->ID);
$criteria->compare('name',$this->name,true);
$criteria->order = 'ID DESC';


$dataProvider=new CActiveDataProvider('Nameofclass', array(
    'criteria'=>$criteria
));
于 2012-06-28T18:31:17.857 回答
2

在您的类中在您的 search() 下添加此行,以按 id 降序显示记录。

$criteria->order = 'ID DESC';

于 2014-01-02T14:52:09.973 回答
1

也可以使用defaultSort来设置默认

$criteria=new CDbCriteria;

$criteria->compare('ID',$this->ID);
$criteria->compare('name',$this->name,true);   

$dataProvider=new CActiveDataProvider('Nameofclass', array(
    'criteria'=>$criteria,
    'sort'=>array(
        'defaultOrder'=>'ID DESC',
    ),
));
于 2012-06-28T22:52:45.347 回答
0
 $criteria = new CDbCriteria;
    $criteria->compare('id', $this->id);
    $criteria->compare('date', $this->date, true);
    $criteria->compare('last', $this->last, true);
    $criteria->compare('first', $this->first, true);
    //$criteria->order = 'id DESC'; // thats not works, if you use CSORT

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
        'pagination' => array(
            'pageSize' => 20
        ),
        'sort' => array(
                'attributes' => array(
                     'id' => array("asc" => 'id', "desc" => 'id desc'),
                     'last' => array("asc" => 'last', "desc" => 'last desc'),
                     'first' => array("asc" => 'first', "desc" => 'first desc'),
                     'date' => array("asc" => 'date', "desc" => 'date desc'),
                ),//attributes array
               'defaultOrder' => 'id DESC',//thats the clue, defaultOrder is on the sort array
        ),//sort array
      ));
于 2014-07-20T20:44:59.227 回答