我为模型常见问题
生成了giix crud控制器:
public function actionAdmin() {
$model = new Faq('search');
$model->unsetAttributes();
if (isset($_GET['Faq']))
$model->setAttributes($_GET['Faq']);
$this->render('admin', array(
'model' => $model,
));
}
查看 - 管理员:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'faq-grid',
'dataProvider' => $model->search(array('order'=>'order ASC')),
'filter' => $model,
'columns' => array(
'order',
'question',
'answer',
array(
'class' => 'CButtonColumn',
),
),
)); ?>
我想按字段“订单”订购商品,所以我添加了
array('order'=>'order ASC')
至$model->search();
,但这并没有改变任何事情。错误在哪里?
答案 0 :(得分:8)
更新模型中的search()函数并添加以下代码
'criteria'=>$criteria,
//add here
'sort'=>array(
'defaultOrder'=>'order ASC',
),
和内部管理视图将数据提供者更改为此
'dataProvider' => $model->search(),
答案 1 :(得分:5)
获取CActiveDataProvider
$dataProvider = $model->search();
并编辑属性&#34;排序&#34;
$dataProvider->sort = array(
'defaultOrder'=>'order ASC'
);
答案 2 :(得分:1)
在“搜索($ order)”方法中,您可以输入:
$criteria=new CDbCriteria;
$criteria->order = $order; // $order = 'order ASC' in your example.
return new CActiveDataProvider(YourModel::model(), array(
'criteria'=>$criteria,
));
答案 3 :(得分:1)
public function search()
{
$criteria = new CDbCriteria;
// build your criteria here
// $criteria->order = 'something'; would be needed if you did `find?()` to return ordered array
// Because CGridView|CListView uses data provider ...
return new CActiveDataProvider( $this, array(
'criteria' => $criteria,
'sort' => array(
'attributes' => array(
'*',
),
// ... so you need to set the default order for order to work
'defaultOrder' => array(
'order' => CSort::SORT_ASC,
),
),
));
}