Skip to content

Commit

Permalink
Merge pull request #2204 from jenssegers/query-builder-revert
Browse files Browse the repository at this point in the history
[3.8] Fix query builder regressions
  • Loading branch information
Smolevich committed Feb 21, 2021
2 parents fe896a1 + 7e472bb commit 09fcda8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 1 addition & 12 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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'])) {
Expand All @@ -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 {
Expand Down
39 changes: 13 additions & 26 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit 09fcda8

Please sign in to comment.