API 平台 Symfony:只得到 30 个结果分页

问题描述

我正在使用带有 Angular 客户端的 Symfony API。尝试从表 Quotation 中获取数据时,当我想要获取所有内容时,最多只能得到 30 个结果。我在 API 上使用 Sellsy。在 SellsyService.PHP 中,我想当我尝试获取报价时会调用函数

public function getQuotations()
    {
        $devisRequest = array(
            'method' => 'Document.getList','params' => array(
                'doctype' => "estimate",'pagination' => array(
                    'nbperpage' => 4999,),'search' => array(
                    //'steps' => ['sent','accepted']
                    'steps' => explode(
                        ",","draft,due,payinprogress,paid,late,sent,read,accepted,contested,expired,advanced,partialinvoiced,invoiced,stored,spent,partialspend,refused"
                    ),);
        $devislist = $this->sellsy->requestApi($devisRequest);

        return $devislist;
    }

我真的不知道去哪里找,我没有开发这个 API。在项目中查找“30”时,可以在缓存中看到:

    'api_platform.eager_loading.max_joins' => 30,'api_platform.collection.pagination.items_per_page' => 30,

不知道是不是和我的问题有关。这是配置 api_platform.yaml:

api_platform:
    swagger:
        api_keys:
            apiKey:
                name: Authorization
                type: header
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']

这 30 个限制我应该喜欢哪里?

解决方法

API 平台有 pagination

如您所料,默认为 30。

API Platform Core 原生支持分页集合。分页 默认情况下为所有集合启用。每个集合包含 每页 30 项

所以您有多种选择来解决您的问题。

  1. 客户端应使用您的分页。所以在你的后端没有什么可改变的,只是在查看你的报价时查询一个页面。

否则,您可以更新配置以禁用分页。

From the documentation

全局禁用分页

select tv.*
from `db.catagory.test_votes_ds` tv left join
     `db.catagory.ISO-Alpha` a
     on LOWER(tv.country) = LOWER(a.Country)
where a.Country IS NULL;

禁用特定资源的分页

# api/config/packages/api_platform.yaml
api_platform:
    defaults:
        pagination_enabled: false

You could also let the client decide.

或者如果您不想禁用它,而是增加每页的项目数:

全局更改每页的项目数

<?php
// api/src/Entity/Quotation.php

use ApiPlatform\Core\Annotation\ApiResource;

/**
 * @ApiResource(attributes={"pagination_enabled"=false})
 */
class Quotation
{
    // ...
}

更改特定资源每页的项目数

# api/config/packages/api_platform.yaml
api_platform:
    defaults:
        pagination_items_per_page: 100 # Default value

您还可以执行其他操作,例如部分分页或更改客户端每页的项目数,但我建议您只查看 documentation