api-platform icon indicating copy to clipboard operation
api-platform copied to clipboard

Built-in OR Filter

Open Mareva-dev opened this issue 3 years ago • 3 comments

Hello,

I just wanted to know if you planned to add a built-in filter which allows us to apply all query parameters using the OR logic. I saw a lot of custom filters out there but nothing really up to date or well documented.

//...
 * @ApiFilter(RangeFilter::class, properties={"productOptions.capacity"})
 * @ApiFilter(OrFilter::class, properties={"productOptions.capacity"})
//...

For example.

Moreover,

The following request works well: https://127.0.0.1:8000/api/products?productOptions.capacity[gt]=10&productOptions.capacity[lt]=20

This one as well: https://127.0.0.1:8000/api/products?productOptions.capacity[between]=400..500

But this one is not working at all: https://127.0.0.1:8000/api/products?productOptions.capacity[between]=10..20 It returns all results (filter is not applied).

is range filter [Between] broken ?

Here is how i declare it :

/**
 * Product.php
 * ...
 * @ApiFilter(SearchFilter::class, properties={"brand", "categories", "type", "productOptions.poolType", "productOptions.packaging"})
 * @ApiFilter(RangeFilter::class, properties={"productOptions.capacity"})
 */



   /**
     * ProductOptions.php
     * ...
     * @ORM\Column(type="float", nullable=true)
     * @Groups({"product_options_read", "product_options_write", "products_read", "products_write"})
     */
    private $capacity;

Mareva-dev avatar Sep 10 '21 09:09 Mareva-dev

Hi,

Do you try this? https://github.com/metaclass-nl/filter-bundle

ngrdanjski avatar Sep 10 '21 09:09 ngrdanjski

Hi,

Do you try this? https://github.com/metaclass-nl/filter-bundle

Thank you for your answer.

I did not but i don't think it can help me. For example: https://127.0.0.1:8000/api/products?productOptions.weight[between]=1..2&productOptions.weight[between]=4..5 is used to get product options with weight between 1..2 and 4..5. What if i want product options of both cases. It seems to me that i cannot do that using the syntax of the metaclass-nl/filter-bundle.

Mareva-dev avatar Sep 10 '21 10:09 Mareva-dev

As the readme of my filter-bundle explains, if you nest in parameters in and or or you can have multiple criteria for the same property using []. Like https://127.0.0.1:8000/api/products?or[][productOptions.weight][between]=1..2&or[][productOptions.weight][between]=4..5. However, when productOptions is a to many association you may need a nested properties workaround, see the readme.

Other options for OR filters (not built-in) can be found at Issue "API Platform "OR" and "IN" filters #639". And there is an idea to add support the composition of filters from server-side configuration, see RFC Filter composition #2400.

metaclass-nl avatar Dec 23 '21 12:12 metaclass-nl

https://github.com/api-platform/core/issues/2400

soyuka avatar Sep 05 '23 12:09 soyuka