From 1d74dc3d3df9f7a579b343f3109160762050ca01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 26 Jul 2023 11:13:34 +0200 Subject: [PATCH] PHPORM-64 Remove Query\Builder::whereAll (#16) --- CHANGELOG.md | 1 + README.md | 11 +++++++++++ src/Query/Builder.php | 29 ----------------------------- tests/Query/BuilderTest.php | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39e9875f7..18adfc131 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file. - 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). +- Remove `Query\Builder::whereAll($column, $values)`. Use `Query\Builder::where($column, 'all', $values)` instead. [#16](https://github.com/GromNaN/laravel-mongodb-private/pull/16) by [@GromNaN](https://github.com/GromNaN). ## [3.9.2] - 2022-09-01 diff --git a/README.md b/README.md index 71e7768e5..f00b3a2c7 100644 --- a/README.md +++ b/README.md @@ -483,6 +483,17 @@ Car::where('weight', 300) ### MongoDB-specific operators +In addition to the Laravel Eloquent operators, all available MongoDB query operators can be used with `where`: + +```php +User::where($fieldName, $operator, $value)->get(); +``` + +It generates the following MongoDB filter: +```ts +{ $fieldName: { $operator: $value } } +``` + **Exists** Matches documents that have the specified field. diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 1a0152a95..574bf8f2b 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -530,24 +530,6 @@ public function orderBy($column, $direction = 'asc') return $this; } - /** - * Add a "where all" clause to the query. - * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not - * @return $this - */ - public function whereAll($column, array $values, $boolean = 'and', $not = false) - { - $type = 'all'; - - $this->wheres[] = compact('column', 'type', 'boolean', 'values', 'not'); - - return $this; - } - /** * @inheritdoc * @param list{mixed, mixed}|CarbonPeriod $values @@ -1044,17 +1026,6 @@ protected function compileWheres(): array return $compiled; } - /** - * @param array $where - * @return array - */ - protected function compileWhereAll(array $where): array - { - extract($where); - - return [$column => ['$all' => array_values($values)]]; - } - /** * @param array $where * @return array diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index 8f7d8f851..bc0644909 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -333,6 +333,22 @@ public static function provideQueryBuilderToMql(): iterable ]), ]; + yield 'where all' => [ + ['find' => [['tags' => ['$all' => ['ssl', 'security']]], []]], + fn (Builder $builder) => $builder->where('tags', 'all', ['ssl', 'security']), + ]; + + yield 'where all nested operators' => [ + ['find' => [['tags' => ['$all' => [ + ['$elemMatch' => ['size' => 'M', 'num' => ['$gt' => 50]]], + ['$elemMatch' => ['num' => 100, 'color' => 'green']], + ]]], []]], + fn (Builder $builder) => $builder->where('tags', 'all', [ + ['$elemMatch' => ['size' => 'M', 'num' => ['$gt' => 50]]], + ['$elemMatch' => ['num' => 100, 'color' => 'green']], + ]), + ]; + /** @see DatabaseQueryBuilderTest::testForPage() */ yield 'forPage' => [ ['find' => [[], ['limit' => 20, 'skip' => 40]]],