Skip to content
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

[7.x] Cast primary key to string when $keyType is string #33930

Merged
merged 3 commits into from
Aug 19, 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
8 changes: 8 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public function whereKey($id)
return $this;
}

if ($this->model->getKeyType() === 'string') {
$id = (string) $id;
}

return $this->where($this->model->getQualifiedKeyName(), '=', $id);
}

Expand All @@ -210,6 +214,10 @@ public function whereKeyNot($id)
return $this;
}

if ($this->model->getKeyType() === 'string') {
$id = (string) $id;
}

return $this->where($this->model->getQualifiedKeyName(), '!=', $id);
}

Expand Down
47 changes: 45 additions & 2 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ protected function tearDown(): void
public function testFindMethod()
{
$builder = m::mock(Builder::class.'[first]', [$this->getMockQueryBuilder()]);
$builder->setModel($this->getMockModel());
$model = $this->getMockModel();
$builder->setModel($model);
$model->shouldReceive('getKeyType')->once()->andReturn('int');
$builder->getQuery()->shouldReceive('where')->once()->with('foo_table.foo', '=', 'bar');
$builder->shouldReceive('first')->with(['column'])->andReturn('baz');

Expand Down Expand Up @@ -76,6 +78,7 @@ public function testFindManyMethod()
public function testFindOrNewMethodModelFound()
{
$model = $this->getMockModel();
$model->shouldReceive('getKeyType')->once()->andReturn('int');
$model->shouldReceive('findOrNew')->once()->andReturn('baz');

$builder = m::mock(Builder::class.'[first]', [$this->getMockQueryBuilder()]);
Expand All @@ -91,6 +94,7 @@ public function testFindOrNewMethodModelFound()
public function testFindOrNewMethodModelNotFound()
{
$model = $this->getMockModel();
$model->shouldReceive('getKeyType')->once()->andReturn('int');
$model->shouldReceive('findOrNew')->once()->andReturn(m::mock(Model::class));

$builder = m::mock(Builder::class.'[first]', [$this->getMockQueryBuilder()]);
Expand All @@ -109,7 +113,9 @@ public function testFindOrFailMethodThrowsModelNotFoundException()
$this->expectException(ModelNotFoundException::class);

$builder = m::mock(Builder::class.'[first]', [$this->getMockQueryBuilder()]);
$builder->setModel($this->getMockModel());
$model = $this->getMockModel();
$model->shouldReceive('getKeyType')->once()->andReturn('int');
$builder->setModel($model);
$builder->getQuery()->shouldReceive('where')->once()->with('foo_table.foo', '=', 'bar');
$builder->shouldReceive('first')->with(['column'])->andReturn(null);
$builder->findOrFail('bar', ['column']);
Expand Down Expand Up @@ -1038,11 +1044,25 @@ public function testWhereKeyMethodWithInt()

$int = 1;

$model->shouldReceive('getKeyType')->once()->andReturn('int');
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '=', $int);

$builder->whereKey($int);
}

public function testWhereKeyMethodWithStringZero()
{
$model = new EloquentBuilderTestStubStringPrimaryKey();
$builder = $this->getBuilder()->setModel($model);
$keyName = $model->getQualifiedKeyName();

$int = 0;

$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '=', (string) $int);

$builder->whereKey($int);
}

public function testWhereKeyMethodWithArray()
{
$model = $this->getMockModel();
Expand All @@ -1069,6 +1089,19 @@ public function testWhereKeyMethodWithCollection()
$builder->whereKey($collection);
}

public function testWhereKeyNotMethodWithStringZero()
{
$model = new EloquentBuilderTestStubStringPrimaryKey();
$builder = $this->getBuilder()->setModel($model);
$keyName = $model->getQualifiedKeyName();

$int = 0;

$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '!=', (string) $int);

$builder->whereKeyNot($int);
}

public function testWhereKeyNotMethodWithInt()
{
$model = $this->getMockModel();
Expand All @@ -1077,6 +1110,7 @@ public function testWhereKeyNotMethodWithInt()

$int = 1;

$model->shouldReceive('getKeyType')->once()->andReturn('int');
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '!=', $int);

$builder->whereKeyNot($int);
Expand Down Expand Up @@ -1445,3 +1479,12 @@ class EloquentBuilderTestStubWithoutTimestamp extends Model

protected $table = 'table';
}

class EloquentBuilderTestStubStringPrimaryKey extends Model
{
public $incrementing = false;

protected $table = 'foo_table';

protected $keyType = 'string';
}