Skip to content

Commit 07c0311

Browse files
committed
Fix query builder regressions
This reverts back #2020 and #2127. See explanation here: #2203
1 parent 61302ca commit 07c0311

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
55

6+
## [3.6.8] - 2021-02-21
7+
8+
### Changed
9+
- (Backport) Fix query builder regression [#2204](https://github.com/jenssegers/laravel-mongodb/pull/2204) by [@divine](https://github.com/divine)
10+
611
## [3.6.7] - 2020-12-18
712

813
### Changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,14 @@ These expressions will be injected directly into the query.
597597
User::whereRaw([
598598
'age' => ['$gt' => 30, '$lt' => 40],
599599
])->get();
600+
601+
User::whereRaw([
602+
'$where' => '/.*123.*/.test(this.field)',
603+
])->get();
604+
605+
User::whereRaw([
606+
'$where' => '/.*123.*/.test(this["hyphenated-field"])',
607+
])->get();
600608
```
601609

602610
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.

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,6 @@ protected function compileWhereAll(array $where)
10151015
protected function compileWhereBasic(array $where)
10161016
{
10171017
extract($where);
1018-
$is_numeric = false;
10191018

10201019
// Replace like or not like with a Regex instance.
10211020
if (in_array($operator, ['like', 'not like'])) {
@@ -1027,21 +1026,15 @@ protected function compileWhereBasic(array $where)
10271026

10281027
// Convert to regular expression.
10291028
$regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value));
1030-
$plain_value = $value;
10311029

10321030
// Convert like to regular expression.
10331031
if (!Str::startsWith($value, '%')) {
10341032
$regex = '^' . $regex;
1035-
} else {
1036-
$plain_value = Str::replaceFirst('%', null, $plain_value);
10371033
}
10381034
if (!Str::endsWith($value, '%')) {
10391035
$regex .= '$';
1040-
} else {
1041-
$plain_value = Str::replaceLast('%', null, $plain_value);
10421036
}
10431037

1044-
$is_numeric = is_numeric($plain_value);
10451038
$value = new Regex($regex, 'i');
10461039
} // Manipulate regexp operations.
10471040
elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex'])) {
@@ -1061,11 +1054,7 @@ protected function compileWhereBasic(array $where)
10611054
}
10621055

10631056
if (!isset($operator) || $operator == '=') {
1064-
if ($is_numeric) {
1065-
$query = ['$where' => '/^'.$value->getPattern().'/.test(this["'.$column.'"])'];
1066-
} else {
1067-
$query = [$column => $value];
1068-
}
1057+
$query = [$column => $value];
10691058
} elseif (array_key_exists($operator, $this->conversion)) {
10701059
$query = [$column => [$this->conversion[$operator] => $value]];
10711060
} else {

tests/QueryTest.php

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public function setUp(): void
1616
User::create(['name' => 'Brett Boe', 'age' => 35, 'title' => 'user']);
1717
User::create(['name' => 'Tommy Toe', 'age' => 33, 'title' => 'user']);
1818
User::create(['name' => 'Yvonne Yoe', 'age' => 35, 'title' => 'admin']);
19-
User::create(['name' => 'John Smith', 'user-age' => 28, 'title' => 'member']);
2019
User::create(['name' => 'Error', 'age' => null, 'title' => null]);
2120
}
2221

@@ -42,10 +41,10 @@ public function testWhere(): void
4241
$this->assertCount(1, $users);
4342

4443
$users = User::where('age', '!=', 35)->get();
45-
$this->assertCount(7, $users);
44+
$this->assertCount(6, $users);
4645

4746
$users = User::where('age', '<>', 35)->get();
48-
$this->assertCount(7, $users);
47+
$this->assertCount(6, $users);
4948
}
5049

5150
public function testAndWhere(): void
@@ -70,33 +69,21 @@ public function testLike(): void
7069

7170
$users = User::where('name', 'like', 't%')->get();
7271
$this->assertCount(1, $users);
73-
74-
$users = User::where('age', 'like', '%35%')->get();
75-
$this->assertCount(3, $users);
76-
77-
$users = User::where('age', 'like', '3%')->get();
78-
$this->assertCount(6, $users);
79-
80-
$users = User::where('age', 'like', '%3')->get();
81-
$this->assertCount(4, $users);
82-
83-
$users = User::where('user-age', 'like', '%28')->get();
84-
$this->assertCount(1, $users);
8572
}
8673

8774
public function testNotLike(): void
8875
{
8976
$users = User::where('name', 'not like', '%doe')->get();
90-
$this->assertCount(8, $users);
77+
$this->assertCount(7, $users);
9178

9279
$users = User::where('name', 'not like', '%y%')->get();
93-
$this->assertCount(7, $users);
80+
$this->assertCount(6, $users);
9481

9582
$users = User::where('name', 'not LIKE', '%y%')->get();
96-
$this->assertCount(7, $users);
83+
$this->assertCount(6, $users);
9784

9885
$users = User::where('name', 'not like', 't%')->get();
99-
$this->assertCount(9, $users);
86+
$this->assertCount(8, $users);
10087
}
10188

10289
public function testSelect(): void
@@ -156,7 +143,7 @@ public function testIn(): void
156143
$this->assertCount(6, $users);
157144

158145
$users = User::whereNotIn('age', [33, 35])->get();
159-
$this->assertCount(5, $users);
146+
$this->assertCount(4, $users);
160147

161148
$users = User::whereNotNull('age')
162149
->whereNotIn('age', [33, 35])->get();
@@ -166,7 +153,7 @@ public function testIn(): void
166153
public function testWhereNull(): void
167154
{
168155
$users = User::whereNull('age')->get();
169-
$this->assertCount(2, $users);
156+
$this->assertCount(1, $users);
170157
}
171158

172159
public function testWhereNotNull(): void
@@ -199,7 +186,7 @@ public function testOrder(): void
199186
public function testGroupBy(): void
200187
{
201188
$users = User::groupBy('title')->get();
202-
$this->assertCount(4, $users);
189+
$this->assertCount(3, $users);
203190

204191
$users = User::groupBy('age')->get();
205192
$this->assertCount(6, $users);
@@ -229,11 +216,11 @@ public function testGroupBy(): void
229216
public function testCount(): void
230217
{
231218
$count = User::where('age', '<>', 35)->count();
232-
$this->assertEquals(7, $count);
219+
$this->assertEquals(6, $count);
233220

234221
// Test for issue #165
235222
$count = User::select('_id', 'age', 'title')->where('age', '<>', 35)->count();
236-
$this->assertEquals(7, $count);
223+
$this->assertEquals(6, $count);
237224
}
238225

239226
public function testExists(): void
@@ -331,12 +318,12 @@ public function testPaginate(): void
331318
$results = User::paginate(2);
332319
$this->assertEquals(2, $results->count());
333320
$this->assertNotNull($results->first()->title);
334-
$this->assertEquals(10, $results->total());
321+
$this->assertEquals(9, $results->total());
335322

336323
$results = User::paginate(2, ['name', 'age']);
337324
$this->assertEquals(2, $results->count());
338325
$this->assertNull($results->first()->title);
339-
$this->assertEquals(10, $results->total());
326+
$this->assertEquals(9, $results->total());
340327
$this->assertEquals(1, $results->currentPage());
341328
}
342329

0 commit comments

Comments
 (0)