-
Notifications
You must be signed in to change notification settings - Fork 0
PHPORM-49 Implement Query\Builder::whereNot
by encapsulating into $not
#13
Changes from all commits
3d269ef
70022ab
e6cba45
df20bb6
f732731
a188374
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 |
---|---|---|
|
@@ -50,6 +50,17 @@ public static function provideQueryBuilderToMql(): iterable | |
fn (Builder $builder) => $builder->where('foo', 'bar'), | ||
]; | ||
|
||
yield 'where with single array of conditions' => [ | ||
['find' => [ | ||
['$and' => [ | ||
['foo' => 1], | ||
['bar' => 2], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder->where(['foo' => 1, 'bar' => 2]), | ||
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. Beyond the scope of this PR I'm sure, but I don't understand why this results in an |
||
]; | ||
|
||
yield 'find > date' => [ | ||
['find' => [['foo' => ['$gt' => new UTCDateTime($date)]], []]], | ||
fn (Builder $builder) => $builder->where('foo', '>', $date), | ||
|
@@ -65,6 +76,177 @@ public static function provideQueryBuilderToMql(): iterable | |
fn (Builder $builder) => $builder->limit(10)->offset(5)->select('foo', 'bar'), | ||
]; | ||
|
||
/** @see DatabaseQueryBuilderTest::testBasicWhereNot() */ | ||
yield 'whereNot (multiple)' => [ | ||
['find' => [ | ||
['$and' => [ | ||
['$not' => ['name' => 'foo']], | ||
['$not' => ['name' => ['$ne' => 'bar']]], | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->whereNot('name', 'foo') | ||
->whereNot('name', '<>', 'bar'), | ||
Comment on lines
+89
to
+90
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 is likely beyond the scope of this PR, but there's room for improvement here. Rather than unconditionally wrap the query with
If you agree, perhaps we can track this in a new PHPORM ticket. I wouldn't limit the query optimization to |
||
]; | ||
|
||
/** @see DatabaseQueryBuilderTest::testBasicOrWheres() */ | ||
yield 'where orWhere' => [ | ||
['find' => [ | ||
['$or' => [ | ||
['id' => 1], | ||
['email' => 'foo'], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->where('id', '=', 1) | ||
->orWhere('email', '=', 'foo'), | ||
]; | ||
|
||
/** @see DatabaseQueryBuilderTest::testBasicOrWhereNot() */ | ||
yield 'orWhereNot' => [ | ||
['find' => [ | ||
['$or' => [ | ||
['$not' => ['name' => 'foo']], | ||
['$not' => ['name' => ['$ne' => 'bar']]], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->orWhereNot('name', 'foo') | ||
->orWhereNot('name', '<>', 'bar'), | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]; | ||
|
||
yield 'whereNot orWhere' => [ | ||
['find' => [ | ||
['$or' => [ | ||
['$not' => ['name' => 'foo']], | ||
['name' => ['$ne' => 'bar']], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->whereNot('name', 'foo') | ||
->orWhere('name', '<>', 'bar'), | ||
]; | ||
|
||
/** @see DatabaseQueryBuilderTest::testWhereNot() */ | ||
yield 'whereNot callable' => [ | ||
['find' => [ | ||
['$not' => ['name' => 'foo']], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->whereNot(fn (Builder $q) => $q->where('name', 'foo')), | ||
]; | ||
|
||
yield 'where whereNot' => [ | ||
['find' => [ | ||
['$and' => [ | ||
['name' => 'bar'], | ||
['$not' => ['email' => 'foo']], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->where('name', '=', 'bar') | ||
->whereNot(function (Builder $q) { | ||
$q->where('email', '=', 'foo'); | ||
}), | ||
]; | ||
|
||
yield 'whereNot (nested)' => [ | ||
['find' => [ | ||
['$not' => [ | ||
'$and' => [ | ||
['name' => 'foo'], | ||
['$not' => ['email' => ['$ne' => 'bar']]], | ||
], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->whereNot(function (Builder $q) { | ||
$q->where('name', '=', 'foo') | ||
->whereNot('email', '<>', 'bar'); | ||
}), | ||
]; | ||
|
||
yield 'orWhere orWhereNot' => [ | ||
['find' => [ | ||
['$or' => [ | ||
['name' => 'bar'], | ||
['$not' => ['email' => 'foo']], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->orWhere('name', '=', 'bar') | ||
->orWhereNot(function (Builder $q) { | ||
$q->where('email', '=', 'foo'); | ||
}), | ||
]; | ||
|
||
yield 'where orWhereNot' => [ | ||
['find' => [ | ||
['$or' => [ | ||
['name' => 'bar'], | ||
['$not' => ['email' => 'foo']], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->where('name', '=', 'bar') | ||
->orWhereNot('email', '=', 'foo'), | ||
]; | ||
|
||
/** @see DatabaseQueryBuilderTest::testWhereNotWithArrayConditions() */ | ||
yield 'whereNot with arrays of single condition' => [ | ||
['find' => [ | ||
['$not' => [ | ||
'$and' => [ | ||
['foo' => 1], | ||
['bar' => 2], | ||
], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->whereNot([['foo', 1], ['bar', 2]]), | ||
]; | ||
|
||
yield 'whereNot with single array of conditions' => [ | ||
['find' => [ | ||
['$not' => [ | ||
'$and' => [ | ||
['foo' => 1], | ||
['bar' => 2], | ||
], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->whereNot(['foo' => 1, 'bar' => 2]), | ||
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 also relates to my earlier comment about query optimization. |
||
]; | ||
|
||
yield 'whereNot with arrays of single condition with operator' => [ | ||
['find' => [ | ||
['$not' => [ | ||
'$and' => [ | ||
['foo' => 1], | ||
['bar' => ['$lt' => 2]], | ||
], | ||
]], | ||
[], // options | ||
]], | ||
fn (Builder $builder) => $builder | ||
->whereNot([ | ||
['foo', 1], | ||
['bar', '<', 2], | ||
]), | ||
]; | ||
|
||
/** @see DatabaseQueryBuilderTest::testOrderBys() */ | ||
yield 'orderBy multiple columns' => [ | ||
['find' => [[], ['sort' => ['email' => 1, 'age' => -1]]]], | ||
|
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.
$where['boolean']
can beand
,or
,and not
oror not
. It is used as-this into SQL queries. I tried several implementations to split into vars that are easier to use later, but usingstr_
functions is the simplest.