-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPORM-90 Fix whereNot to use $nor
#2624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1053,8 +1053,9 @@ protected function compileWheres(): array | |
$method = 'compileWhere' . $where['type']; | ||
$result = $this->{$method}($where); | ||
|
||
// Negate the expression | ||
if (str_ends_with($where['boolean'], 'not')) { | ||
$result = ['$not' => $result]; | ||
$result = ['$nor' => [$result]]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see PHPORM-90 linked to anything. This this bug predate us taking over the library, or was it introduced during some recent query refactoring? If the latter, we should link it up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I implemented |
||
} | ||
|
||
// Wrap the where with an $or operator. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
|
||
use DateTimeImmutable; | ||
use LogicException; | ||
use MongoDB\BSON\Regex; | ||
use MongoDB\Laravel\Eloquent\Builder; | ||
use MongoDB\Laravel\Tests\Models\Birthday; | ||
use MongoDB\Laravel\Tests\Models\Scoped; | ||
use MongoDB\Laravel\Tests\Models\User; | ||
|
@@ -154,6 +156,48 @@ public function testSelect(): void | |
$this->assertNull($user->age); | ||
} | ||
|
||
public function testWhereNot(): void | ||
{ | ||
// implicit equality operator | ||
$users = User::whereNot('title', 'admin')->get(); | ||
$this->assertCount(6, $users); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is failing if I run it on version 3.9.3: the generated query was There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I realized this when originally reviewing PHPORM-49, but the fact that earlier versions completely ignored negations with no feedback to the user sounds like pretty disastrous bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like someone asked for this feature: #2418 |
||
|
||
// nested query | ||
$users = User::whereNot(fn (Builder $builder) => $builder->where('title', 'admin'))->get(); | ||
$this->assertCount(6, $users); | ||
|
||
// double negation | ||
$users = User::whereNot('title', '!=', 'admin')->get(); | ||
$this->assertCount(3, $users); | ||
|
||
// nested negation | ||
$users = User::whereNot(fn (Builder $builder) => $builder | ||
->whereNot('title', 'admin'))->get(); | ||
$this->assertCount(3, $users); | ||
|
||
// explicit equality operator | ||
$users = User::whereNot('title', '=', 'admin')->get(); | ||
$this->assertCount(6, $users); | ||
|
||
// custom query operator | ||
$users = User::whereNot('title', ['$in' => ['admin']])->get(); | ||
$this->assertCount(6, $users); | ||
|
||
// regex | ||
$users = User::whereNot('title', new Regex('^admin$'))->get(); | ||
$this->assertCount(6, $users); | ||
|
||
// equals null | ||
$users = User::whereNot('title', null)->get(); | ||
$this->assertCount(8, $users); | ||
|
||
// nested $or | ||
$users = User::whereNot(fn (Builder $builder) => $builder | ||
->where('title', 'admin') | ||
->orWhere('age', 35))->get(); | ||
$this->assertCount(5, $users); | ||
} | ||
|
||
public function testOrWhere(): void | ||
{ | ||
$users = User::where('age', 13)->orWhere('title', 'admin')->get(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
$result
always going to be an<expression>
at this point, and never an<operator-expression>
as it expected by $not?This is related to my other comment, in that I'm curious how this wasn't discovered sooner unless it was only recently introduced. This also hints at a gap in functional test coverage running generated queries on a live server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
compileWhere*
function always return expressions with$and
,$or
or a field name, and now$nor
.