Skip to content

[10.x] Fixes on nesting operations performed while applying scopes. - #50207

Merged
taylorotwell merged 2 commits into
laravel:10.xfrom
guilhem-delaitre:10.x
Feb 25, 2024
Merged

taylorotwell merged 2 commits into
laravel:10.xfrom
guilhem-delaitre:10.x

Conversation

@guilhem-delaitre

@guilhem-delaitre guilhem-delaitre commented Feb 23, 2024 •

Copy link
Copy Markdown
Contributor

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 ->groupWhereSliceForScope function).

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 is or-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 or appears as one of the where clause, then nesting occurs. But if only a or not appears, then nesting doesn't happens, which is inconsistent for the very reason the nesting happens on or clause : grouping if there is lower priority operator.
    => fixed by grouping if or is contained in any one of the potentially grouped where clauses, rather than one exactly matching or.

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 and between 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 :

public function testLocalScopeNestingDoesntDoubleFirstWhereClauseNegation()
    {
        $model = new EloquentLocalScopesTestModel;
        $query = $model
            ->newQuery()
            ->whereNot('firstWhere', true)
            ->orWhere('secondWhere', true)
            ->active();

        $this->assertSame('select * from "table" where (not "firstWhere" = ? or "secondWhere" = ?) and "active" = ?', $query->toSql());
        $this->assertEquals([true, true, true], $query->getBindings());
    }

    public function testLocalScopeNestingGroupsOrNotWhereClause()
    {
        $model = new EloquentLocalScopesTestModel;
        $query = $model
            ->newQuery()
            ->where('firstWhere', true)
            ->orWhereNot('secondWhere', true)
            ->active();

        $this->assertSame('select * from "table" where ("firstWhere" = ? or not "secondWhere" = ?) and "active" = ?', $query->toSql());
        $this->assertEquals([true, true, true], $query->getBindings());
    }

Results :

Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'select * from "table" where (not "firstWhere" = ? or "secondWhere" = ?) and "active" = ?'
+'select * from "table" where not (not "firstWhere" = ? or "secondWhere" = ?) and "active" = ?'

/home/archie/projects/laravel-framework/tests/Database/DatabaseEloquentLocalScopesTest.php:74

2) Illuminate\Tests\Database\DatabaseEloquentLocalScopesTest::testLocalScopeNestingGroupsOrNotWhereClause
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'select * from "table" where ("firstWhere" = ? or not "secondWhere" = ?) and "active" = ?'
+'select * from "table" where "firstWhere" = ? or not "secondWhere" = ? and "active" = ?'```

The unintuitive results speaks for themselves as far as I'm concerned.

…"or" groups but not "or not" groups and doubling first where clause negation)
@github-actions

Copy link
Copy Markdown

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.

@crynobone crynobone changed the title Fixes on nesting operations performed while applying scopes. [10.x] Fixes on nesting operations performed while applying scopes. Feb 23, 2024
@guilhem-delaitre
guilhem-delaitre marked this pull request as ready for review February 25, 2024 11:59
@taylorotwell
taylorotwell merged commit 7af1994 into laravel:10.x Feb 25, 2024
@taylorotwell

Copy link
Copy Markdown
Member

Thanks

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.

Model::whereNot()->orWhere() incorrectly modifies query structure with SoftDeletes trait or inside a Scope.

2 participants