i’m trying to get all data from database within a rest call but i get only 20 of them, can get by paginating but need all items
how to set it up to get all data from database?
i’m trying to get all data from database within a rest call but i get only 20 of them, can get by paginating but need all items
how to set it up to get all data from database?
You can set pagination as false so that you can display all records at once:
$dataProvider = new ActiveDataProvider([
'query' => User::find()->all(),
'pagination' => false,
]);
Already tried that - same result
Can you show me your Search Model and Controller Action?
When you use the ActiveController (rest guide) you can try this.
$actions['index'] = [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'prepareDataProvider' => function () {
return new ActiveDataProvider([
'query' => $this->modelClass::find(),
'pagination' => false,
]);
},
];
Here is my Model and Controller:
<?php
namespace api\modules\v1\models;
use Yii;
use \yii\db\ActiveRecord;
use \yii\web\Link;
use \yii\web\Linkable;
use \yii\helpers\Url;
class Brand extends ActiveRecord implements Linkable
{
public static function tableName()
{
return 'vbrand';
}
public static function primaryKey()
{
return ['b_id'];
}
public function extraFields()
{
return ['models'];
}
public function getModels(){
return $this->hasMany(Model::className(), ['m_b_id' => 'b_id']);
}
public function fields()
{
return [
//'id' => 'b_id',
'name' => 'b_name',
];
}
public function getLinks()
{
return [
Link::REL_SELF => Url::to(['brands', 'id' => $this->b_id], true),
];
}
}
<?php
namespace api\modules\v1\controllers;
use api\modules\v1\models\Brand;
use yii\rest\ActiveController;
use yii\data\ActiveDataProvider;
class BrandController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Brand';
public function actions()
{
$actions = parent::actions();
unset($actions['create'], $actions['update'], $actions['delete']);
$actions['index'] = [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'prepareDataProvider' => function () {
return new ActiveDataProvider([
'query' => $this->modelClass::find(),
'pagination' => false,
]);
},
];
return $actions;
// return array_merge(
// parent::actions(),
// [
// 'index' => [
// 'class' => 'yii\rest\IndexAction',
// 'modelClass' => $this->modelClass,
// 'checkAccess' => [$this, 'checkAccess'],
// 'prepareDataProvider' => function ($action) {
// $model = new $this->modelClass;
// $query = $model::find();
// $dataProvider = new ActiveDataProvider(['query' => $query]);
//// $model->setAttribute('title', @$_GET['title']);
//// $query->andFilterWhere(['like', 'title', $model->title]);
// return $dataProvider;
// }
// ]
// ]
// );
}
/*public function actionIndex()
{
return new ActiveDataProvider([
'query' => Brand::find()->all(),
'pagination' => false,
]);
}*/
}
it works now, don’t know why it was not working
the final changes of code:
<?php
namespace api\modules\v1\models;
use Yii;
use \yii\db\ActiveRecord;
class Brand extends ActiveRecord
{
public static function tableName()
{
return 'vbrand';
}
public static function primaryKey()
{
return ['b_id'];
}
public function extraFields()
{
return ['models'];
}
public function getModels(){
return $this->hasMany(Model::className(), ['m_b_id' => 'b_id']);
}
public function fields()
{
return [
'name' => 'b_name',
];
}
}
<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
use yii\data\ActiveDataProvider;
class BrandController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Brand';
public function actions()
{
$actions = parent::actions();
unset($actions['create'], $actions['update'], $actions['delete']);
$actions['index'] = [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'prepareDataProvider' => function () {
$model = new $this->modelClass;
$query = $model::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
return $dataProvider;
},
];
return $actions;
}
}
Try this
return User::find()->all();
Thank you so much… you saved my time