Skip to content

Commit

Permalink
Merge pull request mongodb#1829 from ahmedsayedabdelsalam/master
Browse files Browse the repository at this point in the history
fix filtering with operator not like issue
  • Loading branch information
mnphpexpert committed Sep 13, 2019
2 parents 22996c2 + e2c9762 commit 60cee7f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -978,9 +978,13 @@ protected function compileWhereBasic(array $where)
{
extract($where);

// Replace like with a Regex instance.
if ($operator == 'like') {
$operator = '=';
// Replace like or not like with a Regex instance.
if (in_array($operator, ['like', 'not like'])) {
if ($operator === 'not like') {
$operator = 'not';
} else {
$operator = '=';
}

// Convert to regular expression.
$regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value));
Expand Down
15 changes: 15 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ public function testLike(): void
$this->assertCount(1, $users);
}

public function testNotLike(): void
{
$users = User::where('name', 'not like', '%doe')->get();
$this->assertCount(7, $users);

$users = User::where('name', 'not like', '%y%')->get();
$this->assertCount(6, $users);

$users = User::where('name', 'not LIKE', '%y%')->get();
$this->assertCount(6, $users);

$users = User::where('name', 'not like', 't%')->get();
$this->assertCount(8, $users);
}

public function testSelect(): void
{
$user = User::where('name', 'John Doe')->select('name')->first();
Expand Down

0 comments on commit 60cee7f

Please sign in to comment.