Skip to content

[3.x] Fix when column name includes hyphen (-) #2127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ protected function compileWhereBasic(array $where)

if (!isset($operator) || $operator == '=') {
if ($is_numeric) {
$query = ['$where' => '/^'.$value->getPattern().'/.test(this.'.$column.')'];
$query = ['$where' => '/^'.$value->getPattern().'/.test(this["'.$column.'"])'];
} else {
$query = [$column => $value];
}
Expand Down
30 changes: 17 additions & 13 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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 @@ -41,10 +42,10 @@ public function testWhere(): void
$this->assertCount(1, $users);

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

$users = User::where('age', '<>', 35)->get();
$this->assertCount(6, $users);
$this->assertCount(7, $users);
}

public function testAndWhere(): void
Expand Down Expand Up @@ -78,21 +79,24 @@ public function testLike(): void

$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(7, $users);
$this->assertCount(8, $users);

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

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

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

public function testSelect(): void
Expand Down Expand Up @@ -152,7 +156,7 @@ public function testIn(): void
$this->assertCount(6, $users);

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

$users = User::whereNotNull('age')
->whereNotIn('age', [33, 35])->get();
Expand All @@ -162,7 +166,7 @@ public function testIn(): void
public function testWhereNull(): void
{
$users = User::whereNull('age')->get();
$this->assertCount(1, $users);
$this->assertCount(2, $users);
}

public function testWhereNotNull(): void
Expand Down Expand Up @@ -195,7 +199,7 @@ public function testOrder(): void
public function testGroupBy(): void
{
$users = User::groupBy('title')->get();
$this->assertCount(3, $users);
$this->assertCount(4, $users);

$users = User::groupBy('age')->get();
$this->assertCount(6, $users);
Expand Down Expand Up @@ -225,11 +229,11 @@ public function testGroupBy(): void
public function testCount(): void
{
$count = User::where('age', '<>', 35)->count();
$this->assertEquals(6, $count);
$this->assertEquals(7, $count);

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

public function testExists(): void
Expand Down Expand Up @@ -327,12 +331,12 @@ public function testPaginate(): void
$results = User::paginate(2);
$this->assertEquals(2, $results->count());
$this->assertNotNull($results->first()->title);
$this->assertEquals(9, $results->total());
$this->assertEquals(10, $results->total());

$results = User::paginate(2, ['name', 'age']);
$this->assertEquals(2, $results->count());
$this->assertNull($results->first()->title);
$this->assertEquals(9, $results->total());
$this->assertEquals(10, $results->total());
$this->assertEquals(1, $results->currentPage());
}

Expand Down