用于休息 api 调用的 Yii2 数据提供程序
目前我正在使用 yii2 数组 dataprovider
来列出来自 rest api 的数据.我们有超过 1 万条记录.每个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 dataprovider?否则我如何为这个 rest API 实现分页?
Is there any specific rest api dataprovider in yii2? else how can i implement pagination for this rest API?
dataprovider
,在 Yii2
中已经支持分页.假设您的服务通过以下代码接收它的参数:
dataprovider
, in Yii2
has support for pagination. Suppose, your service receives it's params with the following code:
$params = Yii::$app->getRequest()->post();
您可以在请求中包含 page
参数,然后进行一些小技巧 (: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,
],
]);
要获取模型,您可以使用 dataprovider
的 getModels()
方法,如下所示:
To get the models, you could use getModels()
method of dataprovider
like following:
$models = $dataProvider->getModels();
如果这能解决您的问题,请告诉我.
Let me know if that solves your problem.