Skip to content

Fix logic of protection against adding same join multiple times#3308

Closed
klamparski wants to merge 1 commit intoapi-platform:mainfrom
klamparski:master
Closed

Fix logic of protection against adding same join multiple times#3308
klamparski wants to merge 1 commit intoapi-platform:mainfrom
klamparski:master

Conversation

@klamparski
Copy link

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tickets -
License MIT
Doc PR -

Hello,

In project which I'm involved we found problems with filtering. Our custom full text filter is filtering by nested properties of associations thus it is trying to add LEFT JOIN.
It works correctly but not in all cases. If before our full text filter any other filter (or extension) is applied and it adds INNER JOIN for association, then our LEFT JOIN is ignored because QueryBuilderHelper::getExistingJoin does not take into account the type of the JOIN and simply returns true if join for this association already exists.

Such behaviour makes filters not reliable, because they can give different results depending on apply ordering.

Moreover in QueryBuilderHelper::addJoinOnce there was also condition which makes all joins type LEFT if there exist already any LEFT join in the query. It also makes filters not reliable. This PR also removes that condition.

@klamparski klamparski changed the base branch from 2.5 to master December 10, 2019 13:36
@klamparski klamparski changed the base branch from master to 2.5 December 10, 2019 13:46
@klamparski klamparski changed the base branch from 2.5 to master December 10, 2019 13:46
Before that commit `QueryBuilderHelper::addJoinOnce` did not allow to add multiple joins for same association even if new join should have different type than already existing. That means that extensions and filters may affects each other depending on the apply order.

Moreover this commit removes forcing left join if in query already exists at least one left join before. That behaviour also could affect filters depending of apply order.
@klamparski klamparski marked this pull request as ready for review December 10, 2019 13:59
@klamparski
Copy link
Author

I see now that my changes make test \ApiPlatform\Core\Tests\Bridge\Doctrine\Orm\Filter\SearchFilterTest::doTestJoinLeft fails. Sorry for that.

Is that really expected that if any LEFT JOIN in query exists then all next joins from filters will be also LEFT? What is the reason for that?

@soyuka
Copy link
Member

soyuka commented Dec 17, 2019

Is that really expected that if any LEFT JOIN in query exists then all next joins from filters will be also LEFT? What is the reason for that?

Because this usually comes from the fact that the relation is nullable. If you're adding an INNER JOIN for example, you won't get the same results as expected as it'll "filter out" every null value.

@teohhanhui
Copy link
Contributor

Is that really expected that if any LEFT JOIN in query exists then all next joins from filters will be also LEFT? What is the reason for that?

I think that's correct. Doing an INNER JOIN after a LEFT JOIN would give incorrect results, as the previous assumption would no longer be correct. This fix was done a long time ago in #1128

Base automatically changed from master to main January 23, 2021 21:59
@alanpoulain
Copy link
Member

It seems this fix is not relevant. I'm closing this PR.
Feel free to open a new PR with another fix.

@alanpoulain alanpoulain closed this Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants