Api platform filter by null value

Viewed 2228

I am looking for a solution to recover data in get according to a parameter which is null (user):

    {
  "@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"
    },

Currently it retrieves all the data in the table while I only want to recover in GET all the data where user = null

Did you know what arguments API Platform requires to do this?.

My entity:

    /**
 * @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
{

Thank you for help.

1 Answers

"The exists filter allows you to select items based on a nullable field value."

You can add exists filters to your Entity class like this:

// ..
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;

/**
 * @ApiResource(
 * ..
 * )
 * @ApiFilter(ExistsFilter::class, properties={"user"})
 */
class ShippingCost

Then you can fetch them with something like:

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

Filters work on the default DataProvider of all collectionOperations with method GET of the Entity type with the @ApiFilter tag. Wheather it will also work on your operation "getCustomPrices" depends on wheater your controller uses the provided data. But because its config in your ApiResource tag does not contain "read"=false i guess it does.

Related