Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

PHPORM-67 Accept operators prefixed by $ in Query\Builder::orWhere #20

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
- Throw an exception when `Query\Builder::push()` is used incorrectly [#5](https://github.com/GromNaN/laravel-mongodb-private/pull/5) by [@GromNaN](https://github.com/GromNaN).
- Remove public property `Query\Builder::$paginating` [#15](https://github.com/GromNaN/laravel-mongodb-private/pull/15) by [@GromNaN](https://github.com/GromNaN).
- Remove call to deprecated `Collection::count` for `countDocuments` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN).
- Accept operators prefixed by `$` in `Query\Builder::orWhere` [#20](https://github.com/GromNaN/laravel-mongodb-private/pull/20) by [@GromNaN](https://github.com/GromNaN).

## [3.9.2] - 2022-09-01

Expand Down
4 changes: 2 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,10 +917,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
$params = func_get_args();

// Remove the leading $ from operators.
if (func_num_args() == 3) {
if (func_num_args() >= 3) {
$operator = &$params[1];

if (Str::startsWith($operator, '$')) {
if (is_string($operator) && str_starts_with($operator, '$')) {
$operator = substr($operator, 1);
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ public static function provideQueryBuilderToMql(): iterable
fn (Builder $builder) => $builder->limit(10)->offset(5)->select('foo', 'bar'),
];

yield 'where accepts $ in operators' => [
['find' => [
['$or' => [
['foo' => ['$type' => 2]],
['foo' => ['$type' => 4]],
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the patch, the result was ['foo' => '$type'] because Builder::invalidOperator is true and transforms the filter to use $operator as $value.

]],
[], // options
]],
fn (Builder $builder) => $builder
->where('foo', '$type', 2)
->orWhere('foo', '$type', 4),
];

/** @see DatabaseQueryBuilderTest::testBasicWhereNot() */
yield 'whereNot (multiple)' => [
['find' => [
Expand Down