Skip to content

make sure $column is string #2593

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 1 commit into from
Sep 5, 2023
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
12 changes: 9 additions & 3 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public function update(array $values, array $options = [])
*/
public function increment($column, $amount = 1, array $extra = [], array $options = [])
{
$query = ['$inc' => [$column => $amount]];
$query = ['$inc' => [(string) $column => $amount]];

if (! empty($extra)) {
$query['$set'] = $extra;
Expand Down Expand Up @@ -797,9 +797,9 @@ public function push($column, $value = null, $unique = false)
}
$query = [$operator => $column];
} elseif ($batch) {
$query = [$operator => [$column => ['$each' => $value]]];
$query = [$operator => [(string) $column => ['$each' => $value]]];
} else {
$query = [$operator => [$column => $value]];
$query = [$operator => [(string) $column => $value]];
}

return $this->performUpdate($query);
Expand Down Expand Up @@ -1004,8 +1004,14 @@ protected function compileWheres(): array
$where['boolean'] = 'or'.(str_ends_with($where['boolean'], 'not') ? ' not' : '');
}

// Column name can be a Stringable object.
if (isset($where['column']) && $where['column'] instanceof \Stringable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the check for Stringable here but not in other places? Is there a reason why we can't always cast to string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed a if statement to check if column is set, so I added the instanceof to avoid reassigning if the value is already a string. In other places, we know $column is set and we are not assigning the casted value.

$where['column'] = (string) $where['column'];
}

// We use different methods to compile different wheres.
$method = "compileWhere{$where['type']}";

$result = $this->{$method}($where);

if (str_ends_with($where['boolean'], 'not')) {
Expand Down
61 changes: 61 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Str;
use Illuminate\Testing\Assert;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Regex;
Expand Down Expand Up @@ -895,4 +896,64 @@ public function testCursor()
$this->assertEquals($data[$i]['name'], $result['name']);
}
}

public function testStringableColumn()
{
DB::collection('users')->insert([
['name' => 'Jane Doe', 'age' => 36, 'birthday' => new UTCDateTime(new \DateTime('1987-01-01 00:00:00'))],
['name' => 'John Doe', 'age' => 28, 'birthday' => new UTCDateTime(new \DateTime('1995-01-01 00:00:00'))],
]);

$nameColumn = Str::of('name');
$this->assertInstanceOf(\Stringable::class, $nameColumn, 'Ensure we are testing the feature with a Stringable instance');

$user = DB::collection('users')->where($nameColumn, 'John Doe')->first();
$this->assertEquals('John Doe', $user['name']);

// Test this other document to be sure this is not a random success to data order
$user = DB::collection('users')->where($nameColumn, 'Jane Doe')->orderBy('natural')->first();
$this->assertEquals('Jane Doe', $user['name']);

// With an operator
$user = DB::collection('users')->where($nameColumn, '!=', 'Jane Doe')->first();
$this->assertEquals('John Doe', $user['name']);

// whereIn and whereNotIn
$user = DB::collection('users')->whereIn($nameColumn, ['John Doe'])->first();
$this->assertEquals('John Doe', $user['name']);

$user = DB::collection('users')->whereNotIn($nameColumn, ['John Doe'])->first();
$this->assertEquals('Jane Doe', $user['name']);

// whereBetween and whereNotBetween
$ageColumn = Str::of('age');
$user = DB::collection('users')->whereBetween($ageColumn, [30, 40])->first();
$this->assertEquals('Jane Doe', $user['name']);

// whereBetween and whereNotBetween
$ageColumn = Str::of('age');
$user = DB::collection('users')->whereNotBetween($ageColumn, [30, 40])->first();
$this->assertEquals('John Doe', $user['name']);

// whereDate
$birthdayColumn = Str::of('birthday');
$user = DB::collection('users')->whereDate($birthdayColumn, '1995-01-01')->first();
$this->assertEquals('John Doe', $user['name']);

$user = DB::collection('users')->whereDate($birthdayColumn, '<', '1990-01-01')
->orderBy($birthdayColumn, 'desc')->first();
$this->assertEquals('Jane Doe', $user['name']);

$user = DB::collection('users')->whereDate($birthdayColumn, '>', '1990-01-01')
->orderBy($birthdayColumn, 'asc')->first();
$this->assertEquals('John Doe', $user['name']);

$user = DB::collection('users')->whereDate($birthdayColumn, '!=', '1987-01-01')->first();
$this->assertEquals('John Doe', $user['name']);

// increment
DB::collection('users')->where($ageColumn, 28)->increment($ageColumn, 1);
$user = DB::collection('users')->where($ageColumn, 29)->first();
$this->assertEquals('John Doe', $user['name']);
}
}