Currently I'm using the yii2 array dataprovider
for listing the datas from rest api. We have more than 10k records. Each rest api call can get only maximum 100 records
, If we want more i need to give the limit and offset for this rest api call.
Is there any specific rest api dataprovider in yii2? else how can i implement pagination for this rest API?
dataprovider
, in Yii2
has support for pagination. Suppose, your service receives it's params with the following code:
$params = Yii::$app->getRequest()->post();
You could include page
parameter in the request, and then do a little hack (:P), like :
if (isset($params ['page'])) {
$_GET['page'] = (int) $params ['page'];
if ($_GET['page'] < 1) {
$_GET['page'] = 1;
}
}
Once you do that, your dataprovider
automatically assigns the value of $_GET
to it's result set. An example of dataprovider:
$dataProvider = new ActiveDataProvider([
'query' => Users::find(),
'pagination' => array('pageSize' => 10),
]);
Or, in your case:
$dataProvider = new ArrayDataProvider([
'allModels' => $query->from('post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
To get the models, you could use getModels()
method of dataprovider
like following:
$models = $dataProvider->getModels();
Let me know if that solves your problem.