metaclass-nl / filter-bundle

Filter bundle for API Platform, Filter Logic
MIT License
50 stars 9 forks source link

Does this work with Collections? #2

Closed JSmythSSG closed 2 years ago

JSmythSSG commented 3 years ago

When I try to filter with nested collections it doesn't seem to return correct amount.

If I try or[contact.id]=11&or[assigned.engineer.id]=44

the count should be 400 but it returns 10 as the inner join on the one to many seems to limit it

jarrodirwin commented 2 years ago

@JSmythSSG Did you ever get this working with nested collections?

metaclass-nl commented 2 years ago

Sorry for the late response.

Looks like SearchFilter did not expect to be used with nested collections in combination with OR. This is strange because IMHO leaving adding of the where expressions to the implementation of FilterInterface::apply suggest implementors may choose for themselves between using ::andWhere or ::orWhere on the QueryBuilder. Given the implementation of these methods later calls will put the expressions from previous calls in parentheses, so if the custom filter is applied after a SearchFilter, this is what will happen.

And the documentation on Filters does not state that the use of ::andWhere is required. So I guess this could be considered a bug of SearchFilter (the other filters that support nesting may contain the same bug). But issue 826 is about a similar problem, it seems in this comment Antoine Bluchet describes the policy that nullable associations should be LEFT joined. But contrary to this, in this comment Teoh Han Hui says the behavior is correct and the issue is closed.

But in issue 639 the problem is also mentioned and that issue is still open. It also refers to an example in this gist. But as long as the built-in filters of Api Platform generate inner joins for properties nested over nullable or and to many associations, entities that are not associated with at least one related entity will not show up, combining them with OR can not change that. So if they want have (an) OR filter, they must reconsider issue 826. As long is they do not close issue 639 i consider the inner joins a bug.

BTW, one to many relationships are not mentioned explicitly in any of these issues and i could not find an issue that does.

metaclass-nl commented 2 years ago

I added two workarounds for the problem that the built-in filters of Api Platform generate inner joins for properties nested over nullable or and to many associations. But because the are quite intrusive and for reasons of backward compatibility you have to activate them explicitly, see the readme under "Nested properties workaround".