I'm trying to display database for each user in descending date order using data provider this works for Yii 1 at controller :
$DataProvider = new CActiveDataProvider('ModelName', array(
'criteria' => array(
'condition' => 'user_id=' . Yii::app()->user->id,
'order' => 'submitted_dt DESC',
),
'pagination' => array(
'pageSize' => 20,
),
));
i try this in 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();
Error i get is unknown property: yii\data\ActiveDataProvider::criteria
.So what is the way to set this condition and order ?
All suggestions are welcomed
The Yii2
's right way would be:
$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();
So, you don't need criteria
in Yii2
as this is not exist anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With