Skip to content

Commit f4f9ac1

Browse files
[6.x] Port whereKey fix and bugfix for null type (#34031)
* [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 * Fix bug with whereKey null Co-authored-by: Steve Thomas <[email protected]>
1 parent 0eb542d commit f4f9ac1

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ public function whereKey($id)
193193
return $this;
194194
}
195195

196+
if ($id !== null && $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 ($id !== null && $this->model->getKeyType() === 'string') {
218+
$id = (string) $id;
219+
}
220+
213221
return $this->where($this->model->getQualifiedKeyName(), '!=', $id);
214222
}
215223

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 73 additions & 2 deletions
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,39 @@ 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+
1045+
/** @group Foo */
1046+
public function testWhereKeyMethodWithStringNull()
1047+
{
1048+
$model = new EloquentBuilderTestStubStringPrimaryKey();
1049+
$builder = $this->getBuilder()->setModel($model);
1050+
$keyName = $model->getQualifiedKeyName();
1051+
1052+
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '=', m::on(function ($argument) {
1053+
return $argument === null;
1054+
}));
1055+
1056+
$builder->whereKey(null);
1057+
}
1058+
10251059
public function testWhereKeyMethodWithArray()
10261060
{
10271061
$model = $this->getMockModel();
@@ -1048,6 +1082,33 @@ public function testWhereKeyMethodWithCollection()
10481082
$builder->whereKey($collection);
10491083
}
10501084

1085+
public function testWhereKeyNotMethodWithStringZero()
1086+
{
1087+
$model = new EloquentBuilderTestStubStringPrimaryKey();
1088+
$builder = $this->getBuilder()->setModel($model);
1089+
$keyName = $model->getQualifiedKeyName();
1090+
1091+
$int = 0;
1092+
1093+
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '!=', (string) $int);
1094+
1095+
$builder->whereKeyNot($int);
1096+
}
1097+
1098+
/** @group Foo */
1099+
public function testWhereKeyNotMethodWithStringNull()
1100+
{
1101+
$model = new EloquentBuilderTestStubStringPrimaryKey();
1102+
$builder = $this->getBuilder()->setModel($model);
1103+
$keyName = $model->getQualifiedKeyName();
1104+
1105+
$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '!=', m::on(function ($argument) {
1106+
return $argument === null;
1107+
}));
1108+
1109+
$builder->whereKeyNot(null);
1110+
}
1111+
10511112
public function testWhereKeyNotMethodWithInt()
10521113
{
10531114
$model = $this->getMockModel();
@@ -1056,6 +1117,7 @@ public function testWhereKeyNotMethodWithInt()
10561117

10571118
$int = 1;
10581119

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

10611123
$builder->whereKeyNot($int);
@@ -1414,3 +1476,12 @@ class EloquentBuilderTestStubWithoutTimestamp extends Model
14141476

14151477
protected $table = 'table';
14161478
}
1479+
1480+
class EloquentBuilderTestStubStringPrimaryKey extends Model
1481+
{
1482+
public $incrementing = false;
1483+
1484+
protected $table = 'foo_table';
1485+
1486+
protected $keyType = 'string';
1487+
}

0 commit comments

Comments
 (0)