-
-
Notifications
You must be signed in to change notification settings - Fork 969
Closed
Labels
Description
I have been testing API Platform with the different filters available. I noticed we can do a lot without creating custom endpoints. However, when it comes to "OR" and "IN" filters I couldn't find anything.
The idea is to have behind queries acting as:
"WHERE property1 = foo OR property2 = bar"
or
"WHERE property1 IN (foo, bar...)"
Should we create a custom filter for each entity where we can have this kind of query or has API Platform some default feature?
luiscmas, emadpres, harentius, c7nj7n, guilhemVB and 16 moreHoussemTN
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
GregoireHebert commentedon Apr 11, 2018
Hi @anacicconi, this is something really missing right now and yes you can indeed create a custom filter at the moment.
There is an issue opened in the api-platform/core#1832 to fill the gap :)
anacicconi commentedon Apr 11, 2018
Thanks for your quick reply @GregoireHebert !
Are developers creating custom endpoints for these cases? Or custom filters are indeed the best option as we can remain restful?
GregoireHebert commentedon Apr 11, 2018
I would go for a custom filter. Maybe @Simperfit has a different POV on this ?
soyuka commentedon Apr 11, 2018
Custom filters it is :) We already reasoned to this in another issue IIRC.
anacicconi commentedon Apr 11, 2018
I think you're talking about this one @soyuka: api-platform/core#398
I read you said this feature should not be on core. However, as it was from June 2017, I wasn't sure if it was up to date.
Thanks ;)
Simperfit commentedon Apr 11, 2018
Same for me, it should be in a custom filter.
But, we are maybe going to implement a where filter, if the RFC is approved. I will prepare the PR anyway ;).
anacicconi commentedon Apr 11, 2018
Can I reuse the issue to ask a question about the custom filters? I have been testing them since I got your replies. Using the Regexp from the documentation, I'm able to reach the filterProperty method when I add the filter to my resource like this:
However, when I try to use the ApiFilter annotation nothing happens:
I tried many combinations of the way I add the filter to the resource and the way I declare my service. There are only two ways I can make this work.
First:
This way I have both the filter in the swagger interface and I reach the filterProperty method.
Second:
This way I don't have the filter in the swagger interface but still if I curl the url with regexp_website as a parameter I reach the filterProperty method.
My service options for both cases:
Even if the first option works, I would prefer to use the ApiFilter annotation. This way I would be able to declare a generic custom filter that I can use in any resource without specifying the properties in the services.yml. And of course I would have the filter in the swagger interface.
Am I missing something here?
teohhanhui commentedon Apr 19, 2018
Does the multi values not work for the
IN
case?https://github.com/api-platform/core/blob/v2.2.5/src/Bridge/Doctrine/Orm/Filter/SearchFilter.php#L251
anacicconi commentedon Apr 19, 2018
Hi @teohhanhui, yes they work. The title of my question is not good. I was starting to deal with filters when I asked it. As you pointed out, the Search Filter checks if it is an array or a single value. If it's an array, it adds a "IN". However, for "OR" queries, I had to do a custom filter.
anacicconi commentedon Sep 5, 2018
Hello,
I'll start this conversation again if it's ok. I did a custom "OR" filter for my app which is basically a copy of the "SearchFilter" with some modifications for the query. It works just fine. However, I think it's a pity to have a copy of another file like that. I was wondering if I could have my custom filter to extend the "SearchFilter". I read in one of your issues that you don't advise people to do that. Is there a reason? Besides the fact that eventually the "SearchFilter" could be updated and break my custom filter.
Moreover, would it be a good idea to change the original "SearchFilter" so the user could choose what kind of operator he wants?
Thanks!
axelvnk commentedon Dec 13, 2018
In case someone is looking for an example of an OR search filter : here it is
https://gist.github.com/axelvnk/edf879af5c7dbd9616a4eeb77c7181a3
masseelch commentedon Jul 11, 2020
I did one myself (more like a full-text kind-of filter thingy).
https://gist.github.com/masseelch/47931f3a745409f8f44c69efa9ecb05c
4 remaining items
metaclass-nl commentedon Dec 18, 2021
One can accomplish similar results (and more) with FilterLogic: combines existing API Platform ORM Filters with AND, OR and NOT according to client request.
john-dufrene-dev commentedon Dec 22, 2021
Hi,
(Sorry for my bad english)
With version 2.7 somes changes are applied,
Someone have an example with 2.7 version of api platform ?
Thank's in advance !
John,
metaclass-nl commentedon Dec 23, 2021
How do i obtian the 2.7 version of api platform ?
metaclass-nl commentedon Nov 14, 2022
Version v3.0.0.rc2 of FilterLogic has been adapted for API Platform Core 3.0 and 2.7 with metadata_backward_compatibility_layer set to false.
BTW, According to https://github.com/api-platform/api-platform/releases there is still no 2.7 version, but let's assume you meant the Core repo (but then this issues should have been created there) then still, the first 2.7 version on https://github.com/api-platform/core/releases is v2.7.0-rc.1 and it is dated jul 20 2022.
drennvinn commentedon Jan 9, 2023
if it can help anyone, i wrote this in order to be able to search on multiple fields with only one query param:
Now you can add
#[ApiFilter(MultipleFieldsSearchFilter::class, properties: ["firstName", "lastName", "TheFieldYouWant"])]
in your entity 👍LaurineLassalle84120 commentedon Feb 26, 2023
@drennvinn
Can you tell me where i can use your code? In which class? In which file? I don't understand
drennvinn commentedon Feb 26, 2023
Sure! here is an improved version which also allows you to search in the subresources.
And on the entity side you can declare it as follows
now you can search in multiple fields with the following query:
books?search=searched string in declared fields..
I think this code can be improved, but I don't have much time at the moment.
AmineOUERTANI commentedon May 5, 2023
This code may help you solve the problem
Exemple for entity user : #[ApiFilter(OrSearchFilter::class, properties: ['firstname', 'lastname', 'country.name'])]
Uri : /api/user?search=criteria
metaclass-nl commentedon May 7, 2023
@AmineOUERTANI as described in this issue using $queryBuilder->orWhere can cause security issues in combination with extensions. The solution of @drennvinn uses seperate or expressions that are combined with the rest of the query through ->andWhere so will not have this problem.
silverbackdan commentedon May 7, 2023
As I posted above a while ago, this was also another solution I had whereby the 'or' queries are all wrapped within an 'andWhere' - I think it still works.
https://gist.github.com/silverbackdan/0a1753735e07210b3f4365a3100b83b7
AmineOUERTANI commentedon May 9, 2023
@metaclass-nl here is the solution with andwhere
lcottingham commentedon Aug 26, 2023
Do any of these solutions work with GraphQL? Out of the box, they are detected by the graphql schema, but don't appear to be effective on query.
i.e.
the above in gql would return the query as if the
search_
prefixed filters weren't there.luizkim commentedon Sep 28, 2023
In file: api\config\packages\api_platform.yaml
Remove:
parameters:
api_platform:
#metadata_backward_compatibility_layer: false
Just comment this line and the problem is gone: metadata_backward_compatibility_layer