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

Skip to content

Commit 56d2fec

Browse files
authored
[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 99c74a8 commit 56d2fec

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
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']);
@@ -1038,11 +1044,25 @@ public function testWhereKeyMethodWithInt()
10381044

10391045
$int = 1;
10401046

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

10431050
$builder->whereKey($int);
10441051
}
10451052

1053+
public function testWhereKeyMethodWithStringZero()
1054+
{
1055+
$model = new EloquentBuilderTestStubStringPrimaryKey();
1056+
$builder = $this->getBuilder()->setModel($model);
1057+
$keyName = $model->getQualifiedKeyName();
1058+
1059+
$int = 0;
1060+
1061+
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '=', (string) $int);
1062+
1063+
$builder->whereKey($int);
1064+
}
1065+
10461066
public function testWhereKeyMethodWithArray()
10471067
{
10481068
$model = $this->getMockModel();
@@ -1069,6 +1089,19 @@ public function testWhereKeyMethodWithCollection()
10691089
$builder->whereKey($collection);
10701090
}
10711091

1092+
public function testWhereKeyNotMethodWithStringZero()
1093+
{
1094+
$model = new EloquentBuilderTestStubStringPrimaryKey();
1095+
$builder = $this->getBuilder()->setModel($model);
1096+
$keyName = $model->getQualifiedKeyName();
1097+
1098+
$int = 0;
1099+
1100+
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '!=', (string) $int);
1101+
1102+
$builder->whereKeyNot($int);
1103+
}
1104+
10721105
public function testWhereKeyNotMethodWithInt()
10731106
{
10741107
$model = $this->getMockModel();
@@ -1077,6 +1110,7 @@ public function testWhereKeyNotMethodWithInt()
10771110

10781111
$int = 1;
10791112

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

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

14461480
protected $table = 'table';
14471481
}
1482+
1483+
class EloquentBuilderTestStubStringPrimaryKey extends Model
1484+
{
1485+
public $incrementing = false;
1486+
1487+
protected $table = 'foo_table';
1488+
1489+
protected $keyType = 'string';
1490+
}

0 commit comments

Comments
 (0)
0