【问题标题】:Api platform filter by null valueApi 平台按空值过滤
【发布时间】:2020-11-11 04:26:47
【问题描述】:

我正在寻找一种解决方案,根据为 null(用户)的参数恢复 get 中的数据:

    {
  "@context": "\/api\/contexts\/ShippingCost",
  "@id": "\/api\/shipping_costs",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "\/api\/shipping_costs\/1",
      "@type": "ShippingCost",
      "id": 1,
      "minWeight": 0,
      "maxWeight": 251,
      "france": 4.87,
      "domTom": 4.21,
      "aerial": 3.84,
      "zone": {
        "@id": "\/api\/zones\/1",
        "@type": "Zone",
        "id": 1,
        "name": "Guadeloupe",
        "TaxFOB": 35,
        "TaxSurete": 0.2,
        "TaxFuel": 0.77,
        "TaxGuerre": 0.24,
        "Lta": 0,
        "InfoDouane": 24,
        "CreditEnlevement": 0,
        "EntreposageCci": 0.4,
        "EntreposageCciMin": 15,
        "RemiseDoc": 43,
        "Surete": 0,
        "AvanceFond": 0,
        "Tid": 13,
        "Spia": 10,
        "InterTransite": 50
      },
      "user": null
    },
{
      "@id": "\/api\/shipping_costs\/162",
      "@type": "ShippingCost",
      "id": 162,
      "minWeight": 0,
      "maxWeight": 250,
      "france": 3,
      "domTom": 5,
      "aerial": 4,
      "zone": {
        "@id": "\/api\/zones\/4",
        "@type": "Zone",
        "id": 4,
        "name": "Guyane",
        "TaxFOB": 30,
        "TaxSurete": 0.2,
        "TaxFuel": 0.77,
        "TaxGuerre": 0.24,
        "Lta": 34.1,
        "InfoDouane": 24,
        "CreditEnlevement": 0,
        "EntreposageCci": 0.4,
        "EntreposageCciMin": 6,
        "RemiseDoc": 34.5,
        "Surete": 0,
        "AvanceFond": 0,
        "Tid": 0,
        "Spia": 0,
        "InterTransite": 10
      },
      "user": "\/api\/customers\/153"
    },

目前它检索表中的所有数据,而我只想在 GET 中恢复 user = null 的所有数据

您知道 API 平台需要哪些参数来执行此操作吗?

我的实体:

    /**
 * @ApiResource(
 *     attributes={"pagination_enabled"=false},
 *     collectionOperations={
 *      "get"={
 *             "method"="GET",
 *             "normalization_context"={"groups"={"shippingGet", "shippingGetCollection"}},
 *             "access_control"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CUSTOMER')"
 *         },
 *         "getCustomPrices"={
 *             "method"="GET",
 *             "normalization_context"={"groups"={"shippingGetCustomPrice"}},
 *             "access_control"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CUSTOMER')",
 *              "path"="/shipping_costs/{userId}/customPrices",
 *             "route_name"="get_shipping_costs_userid",
 *             "controller"="App\Controller\ShippingCostsController",
 *             "swagger_context" = {
 *                         "parameters" = {
 *                             {
 *                                 "name" = "userId",
 *                                 "in" = "query",
 *                                 "description" = "ID customer",
 *                                 "type" : "string",
 *                             }
 *                         }
 *                      }
 *                 },
 *         "post"={
 *             "method"="POST",
 *             "normalization_context"={"groups"={"shippingPost"}},
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         }
 *     },
 *     itemOperations={
 *         "getItem"={
 *             "method"="GET",
 *             "normalization_context"={"groups"={"shippingGet", "shippingGetItem"}},
 *             "access_control"="is_granted('ROLE_ADMIN') or is_granted('ROLE_CUSTOMER')"
 *         },
 *         "delete"={
 *             "method"="DELETE",
 *             "normalization_context"={"groups"={"shippingDelete"}},
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         },
 *         "put"={
 *             "method"="PUT",
 *             "normalization_context"={"groups"={"shippingPost"}},
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         }
 *     }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\ShippingCostRepository")
 */
class ShippingCost
{

感谢您的帮助。

【问题讨论】:

    标签: api symfony api-platform.com


    【解决方案1】:

    exists filter 允许您根据可为空的字段值选择项目。”

    您可以像这样向您的实体类添加存在过滤器:

    // ..
    use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
    
    /**
     * @ApiResource(
     * ..
     * )
     * @ApiFilter(ExistsFilter::class, properties={"user"})
     */
    class ShippingCost
    

    然后您可以使用以下方式获取它们:

    https://localhost:8443/api/shipping_costs?exists[user]=false。

    过滤器使用带有@ApiFilter 标记的Entity 类型的方法GET 对所有collectionOperations 的默认DataProvider 起作用。它是否也适用于您的操作“getCustomPrices”取决于您的控制器使用提供的数据的程度。但是因为它在你的 ApiResource 标签中的配置不包含 "read"=false 我猜是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 2022-12-23
      • 1970-01-01
      • 2022-11-04
      • 2023-02-11
      • 1970-01-01
      相关资源
      最近更新 更多