8000 [7.x] Cast primary key to string when $keyType is string (#33930) · laravel/framework@c2f3425 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2f3425

Browse files
stevethomasdriesvints
authored andcommitted
[7.x] Cast primary key to string when $keyType is string (#33930)
* Cast primary key to string when $keyType is string * fix test * fix remaining tests
1 parent 0eb542d commit c2f3425

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+8
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ public function whereKey($id)
193193
return $this;
194194
}
195195

196+
if ($this->model->getKeyType() === 'string') {
197+
$id = (string) $id;
198+
}
199+
196200
return $this->where($this->model->getQualifiedKeyName(), '=', $id);
197201
}
198202

@@ -210,6 +214,10 @@ public function whereKeyNot($id)
210214
return $this;
211215
}
212216

217+
if ($this->model->getKeyType() === 'string') {
218+
$id = (string) $id;
219+
}
220+
213221
return $this->where($this->model->getQualifiedKeyName(), '!=', $id);
214222
}
215223

tests/Database/DatabaseEloquentBuilderTest.php

+45-2
6042
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ protected function tearDown(): void
3131
public function testFindMethod()
3232
{
3333
$builder = m::mock(Builder::class.'[first]', [$this->getMockQueryBuilder()]);
34-
$builder->setModel($this->getMockModel());
34+
$model = $this->getMockModel();
35+
$builder->setModel($model);
36+
$model->shouldReceive('getKeyType')->once()->andReturn('int');
3537
$builder->getQuery()->shouldReceive('where')->once()->with('foo_table.foo', '=', 'bar');
3638
$builder->shouldReceive('first')->with(['column'])->andReturn('baz');
3739

@@ -76,6 +78,7 @@ public function testFindManyMethod()
7678
public function testFindOrNewMethodModelFound()
7779
{
7880
$model = $this->getMockModel();
81+
$model->shouldReceive('getKeyType')->once()->andReturn('int');
7982
$model->shouldReceive('findOrNew')->once()->andReturn('baz');
8083

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

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

111115
$builder = m::mock(Builder::class.'[first]', [$this->getMockQueryBuilder()]);
112-
$builder->setModel($this->getMockModel());
116+
$model = $this->getMockModel();
117+
$model->shouldReceive('getKeyType')->once()->andReturn('int');
118+
$builder->setModel($model);
113119
$builder->getQuery()->shouldReceive('where')->once()->with('foo_table.foo', '=', 'bar');
114120
$builder->shouldReceive('first')->with(['column'])->andReturn(null);
115121
$builder->findOrFail('bar', ['column']);
@@ -1017,11 +1023,25 @@ public function testWhereKeyMethodWithInt()
10171023

10181024
$int = 1;
10191025

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

10221029
$builder->whereKey($int);
10231030
}
10241031

1032+
public function testWhereKeyMethodWithStringZero()
1033+
{
1034+
$model = new EloquentBuilderTestStubStringPrimaryKey();
1035+
$builder = $this->getBuilder()->setModel($model);
1036+
$keyName = $model->getQualifiedKeyName();
1037+
1038+
$int = 0;
1039+
1040+
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '=', (string) $int);
1041+
1042+
$builder->whereKey($int);
1043+
}
1044+
10251045
public function testWhereKeyMethodWithArray()
10261046
{
10271047
$model = $this->getMockModel();
@@ -1048,6 +1068,19 @@ public function testWhereKeyMethodWithCollection()
10481068
$builder->whereKey($collection);
10491069
}
10501070

1071+
public function testWhereKeyNotMethodWithStringZero()
1072+
{
1073+
$model = new EloquentBuilderTestStubStringPrimaryKey();
1074+
$builder = $this->getBuilder()->setModel($model);
1075+
$keyName = $model->getQualifiedKeyName();
1076+
1077+
$int = 0; A3E2
1078+
1079+
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '!=', (string) $int);
1080+
1081+
$builder->whereKeyNot($int);
1082+
}
1083+
10511084
public function testWhereKeyNotMethodWithInt()
10521085
{
10531086
$model = $this->getMockModel();
@@ -1056,6 +1089,7 @@ public function testWhereKeyNotMethodWithInt()
10561089

10571090
$int = 1;
10581091

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

10611095
$builder->whereKeyNot($int);
@@ -1414,3 +1448,12 @@ class EloquentBuilderTestStubWithoutTimestamp extends Model
14141448

14151449
protected $table = 'table';
14161450
}
1451+
1452+
class EloquentBuilderTestStubStringPrimaryKey extends Model
1453+
{
1454+
public $incrementing = false;
1455+
1456+
protected $table = 'foo_table';
1457+
1458+
protected $keyType = 'string';
1459+
}

0 commit comments

Comments
 (0)
0