休息API调用Yii2数据提供商 [英] Yii2 data provider for rest api call

查看:282
本文介绍了休息API调用Yii2数据提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用的是 yii2阵列的dataProvider 从REST API列出DATAS。我们有超过10K的记录。每个REST API调用可以得到的只有最高100条记录,如果我们想要更多,我需要给限度和这个REST API调用所抵消。

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.

有没有yii2任何具体的REST API数据提供器?要不怎么我能为这个REST API实现分页?

Is there any specific rest api dataprovider in yii2? else how can i implement pagination for this rest API?

推荐答案

的dataProvider Yii2 有支持分页。假设,你的服务接收它具有以下code PARAMS:

dataprovider, in Yii2 has support for pagination. Suppose, your service receives it's params with the following code:

$params = Yii::$app->getRequest()->post();

您可以在请求中包含参数,然后做一个小小的黑客(:P),如:

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;
    }
}

一旦你这样做,你的的dataProvider 自动分配 $的价值_ GET 到它的结果集。数据提供器的一个例子:

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),
        ]);

或者,你的情况:

$dataProvider = new ArrayDataProvider([
    'allModels' => $query->from('post')->all(),
    'sort' => [
        'attributes' => ['id', 'username', 'email'],
    ],
    'pagination' => [
        'pageSize' => 10,
    ],
        ]);

要获得模型,你可以使用 getModels() 的dataProvider 的方法类似如下:

To get the models, you could use getModels() method of dataprovider like following:

$models = $dataProvider->getModels();

让我知道这是否解决您的问题。

Let me know if that solves your problem.

这篇关于休息API调用Yii2数据提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆