Yii2 data provider for rest api call [closed]

可紊 提交于 2019-12-07 17:18:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/33803822/yii2-data-provider-for-rest-api-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!