我尝试使用数据提供程序以降序日期顺序为每个用户显示数据库,这适用于控制器上的Yii 1:
$DataProvider = new CActiveDataProvider('ModelName', array(
'criteria' => array(
'condition' => 'user_id=' . Yii::app()->user->id,
'order' => 'submitted_dt DESC',
),
'pagination' => array(
'pageSize' => 20,
),
));
我在Yii 2中试试这个:
$DataProvider = new ActiveDataProvider([
'query' => ModelName::find(),
'criteria' => [
'condition' => 'user_id=' . Yii::$app->user->identity->id,
'order' => 'submitted_dt DESC',
],
'pagination' => [
'pageSize' => 20,
],
]);
// get posts in the current page
$Model= $DataProvider->getModels();
我得到的错误是未知属性:yii\data\ActiveDataProvider::criteria
。那么设置此条件和顺序的方法是什么?
欢迎所有建议
答案 0 :(得分:14)
Yii2
正确的方法是:
$DataProvider = new ActiveDataProvider([
'query' => ModelName::find()->
where(['user_id'=>Yii::$app->user->identity->id])->
orderBy('submitted_dt DESC'),
'pagination' => [
'pageSize' => 20,
],
]);
// get posts in the current page
$Model= $DataProvider->getModels();
因此,criteria
中不需要Yii2
,因为这不再存在。