Repository navigation
[10.x] Fixes on nesting operations performed while applying scopes. - #50207
Merged
Merged
Conversation
…"or" groups but not "or not" groups and doubling first where clause negation)
…re clause negation when nesting
|
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
guilhem-delaitre
marked this pull request as ready for review
February 25, 2024 11:59
Member
|
Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request was initiated by the following issue : #50078
In which is put forward a problem with nesting when scope are applied.
As a reminder, when a scope is applied, previously existing where clauses and scope where clauses can be grouped together (with parenthesis) depending on some criteria (it's done with
src/Illuminate/Database/Eloquent/Builder.php->groupWhereSliceForScopefunction).From the code doing this operation what is happening is pretty obvious :
if the nesting happens, the first operator of the clauses that are going to be nested is used as operator for the whole groups (which allows for example that when a scope start with a
orWhere, the whole scoped isor-ed with the rest of the query)But if the first operator contains a negation, then the whole group is negated, which is what happens in the issue to which this PR is linked.
=> fixed it by removing negation of the operator applied on the group.
if the logical operator
orappears as one of the where clause, then nesting occurs. But if only aor notappears, then nesting doesn't happens, which is inconsistent for the very reason the nesting happens onorclause : grouping if there is lower priority operator.=> fixed by grouping if
oris contained in any one of the potentially grouped where clauses, rather than one exactly matchingor.I put tests in local scopes because I thought it was simpler, but the problem occurs in exactly the same ways for local and global scopes.
If you ask me, I would use nested queries everytime there is multiple where clauses and always use
andbetween pre-existing where clauses and scope, by the very meaning of the word scope. But I'm not here to fundamentally change the spirit, just fix the issue.Thanks to anyone taking some time to give feedback about those fixes.
For information, here is what happens with the tests I added and before the fixes when performing those tests :
Results :