From dc931475c8456db69652e8d4c83f5d5c24362f48 Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Sat, 20 Feb 2021 01:15:46 +0300 Subject: [PATCH 1/2] Fix query builder regressions This reverts back #2020 and #2127. See explanation here: https://github.com/jenssegers/laravel-mongodb/issues/2203 --- README.md | 8 +++++ src/Jenssegers/Mongodb/Query/Builder.php | 13 +------- tests/QueryTest.php | 39 ++++++++---------------- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index c30100908..808e266ec 100644 --- a/README.md +++ b/README.md @@ -599,6 +599,14 @@ These expressions will be injected directly into the query. User::whereRaw([ 'age' => ['$gt' => 30, '$lt' => 40], ])->get(); + +User::whereRaw([ + '$where' => '/.*123.*/.test(this.field)', +])->get(); + +User::whereRaw([ + '$where' => '/.*123.*/.test(this["hyphenated-field"])', +])->get(); ``` You can also perform raw expressions on the internal MongoCollection object. If this is executed on the model class, it will return a collection of models. diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index e466ea411..afe58e108 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -993,7 +993,6 @@ protected function compileWhereAll(array $where) protected function compileWhereBasic(array $where) { extract($where); - $is_numeric = false; // Replace like or not like with a Regex instance. if (in_array($operator, ['like', 'not like'])) { @@ -1005,21 +1004,15 @@ protected function compileWhereBasic(array $where) // Convert to regular expression. $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); - $plain_value = $value; // Convert like to regular expression. if (!Str::startsWith($value, '%')) { $regex = '^' . $regex; - } else { - $plain_value = Str::replaceFirst('%', null, $plain_value); } if (!Str::endsWith($value, '%')) { $regex .= '$'; - } else { - $plain_value = Str::replaceLast('%', null, $plain_value); } - $is_numeric = is_numeric($plain_value); $value = new Regex($regex, 'i'); } // Manipulate regexp operations. elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex'])) { @@ -1039,11 +1032,7 @@ protected function compileWhereBasic(array $where) } if (!isset($operator) || $operator == '=') { - if ($is_numeric) { - $query = ['$where' => '/^'.$value->getPattern().'/.test(this["'.$column.'"])']; - } else { - $query = [$column => $value]; - } + $query = [$column => $value]; } elseif (array_key_exists($operator, $this->conversion)) { $query = [$column => [$this->conversion[$operator] => $value]]; } else { diff --git a/tests/QueryTest.php b/tests/QueryTest.php index d7c170495..c5cc1152e 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -16,7 +16,6 @@ public function setUp(): void User::create(['name' => 'Brett Boe', 'age' => 35, 'title' => 'user']); User::create(['name' => 'Tommy Toe', 'age' => 33, 'title' => 'user']); User::create(['name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin']); - User::create(['name' => 'John Smith', 'user-age' => 28, 'title' => 'member']); User::create(['name' => 'Error', 'age' => null, 'title' => null]); } @@ -42,10 +41,10 @@ public function testWhere(): void $this->assertCount(1, $users); $users = User::where('age', '!=', 35)->get(); - $this->assertCount(7, $users); + $this->assertCount(6, $users); $users = User::where('age', '<>', 35)->get(); - $this->assertCount(7, $users); + $this->assertCount(6, $users); } public function testAndWhere(): void @@ -70,33 +69,21 @@ public function testLike(): void $users = User::where('name', 'like', 't%')->get(); $this->assertCount(1, $users); - - $users = User::where('age', 'like', '%35%')->get(); - $this->assertCount(3, $users); - - $users = User::where('age', 'like', '3%')->get(); - $this->assertCount(6, $users); - - $users = User::where('age', 'like', '%3')->get(); - $this->assertCount(4, $users); - - $users = User::where('user-age', 'like', '%28')->get(); - $this->assertCount(1, $users); } public function testNotLike(): void { $users = User::where('name', 'not like', '%doe')->get(); - $this->assertCount(8, $users); + $this->assertCount(7, $users); $users = User::where('name', 'not like', '%y%')->get(); - $this->assertCount(7, $users); + $this->assertCount(6, $users); $users = User::where('name', 'not LIKE', '%y%')->get(); - $this->assertCount(7, $users); + $this->assertCount(6, $users); $users = User::where('name', 'not like', 't%')->get(); - $this->assertCount(9, $users); + $this->assertCount(8, $users); } public function testSelect(): void @@ -156,7 +143,7 @@ public function testIn(): void $this->assertCount(6, $users); $users = User::whereNotIn('age', [33, 35])->get(); - $this->assertCount(5, $users); + $this->assertCount(4, $users); $users = User::whereNotNull('age') ->whereNotIn('age', [33, 35])->get(); @@ -166,7 +153,7 @@ public function testIn(): void public function testWhereNull(): void { $users = User::whereNull('age')->get(); - $this->assertCount(2, $users); + $this->assertCount(1, $users); } public function testWhereNotNull(): void @@ -199,7 +186,7 @@ public function testOrder(): void public function testGroupBy(): void { $users = User::groupBy('title')->get(); - $this->assertCount(4, $users); + $this->assertCount(3, $users); $users = User::groupBy('age')->get(); $this->assertCount(6, $users); @@ -229,11 +216,11 @@ public function testGroupBy(): void public function testCount(): void { $count = User::where('age', '<>', 35)->count(); - $this->assertEquals(7, $count); + $this->assertEquals(6, $count); // Test for issue #165 $count = User::select('_id', 'age', 'title')->where('age', '<>', 35)->count(); - $this->assertEquals(7, $count); + $this->assertEquals(6, $count); } public function testExists(): void @@ -331,12 +318,12 @@ public function testPaginate(): void $results = User::paginate(2); $this->assertEquals(2, $results->count()); $this->assertNotNull($results->first()->title); - $this->assertEquals(10, $results->total()); + $this->assertEquals(9, $results->total()); $results = User::paginate(2, ['name', 'age']); $this->assertEquals(2, $results->count()); $this->assertNull($results->first()->title); - $this->assertEquals(10, $results->total()); + $this->assertEquals(9, $results->total()); $this->assertEquals(1, $results->currentPage()); } From 7e472bb85eaa284e22d9d9ad2980ac7edefa4148 Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Sat, 20 Feb 2021 01:40:52 +0300 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e428447a..c551fe370 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +## [3.8.3] - 2021-02-21 + +### Changed +- Fix query builder regression [#2204](https://github.com/jenssegers/laravel-mongodb/pull/2204) by [@divine](https://github.com/divine) + ## [3.8.2] - 2020-12-18 ### Changed