Skip to content

Commit c803376

Browse files
author
bryan
committed
Adds additional check of keyType to avoid using an invalid type for eager load constraints
1 parent d190a63 commit c803376

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Illuminate/Database/Eloquent/Model.php

+10
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,16 @@ public function getIncrementing()
23772377
return $this->incrementing;
23782378
}
23792379

2380+
/**
2381+
* Get the value indicating the auto incrementing key type.
2382+
*
2383+
* @return string
2384+
*/
2385+
public function getKeyType()
2386+
{
2387+
return $this->keyType;
2388+
}
2389+
23802390
/**
23812391
* Set whether IDs are incrementing.
23822392
*

src/Illuminate/Database/Eloquent/Relations/BelongsTo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected function getEagerModelKeys(array $models)
174174
// null or 0 in (depending on if incrementing keys are in use) so the query wont
175175
// fail plus returns zero results, which should be what the developer expects.
176176
if (count($keys) === 0) {
177-
return [$this->related->getIncrementing() ? 0 : null];
177+
return [$this->related->getIncrementing() && $this->related->getKeyType() === 'int' ? 0 : null];
178178
}
179179

180180
return array_values(array_unique($keys));

tests/Database/DatabaseEloquentBelongsToTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ public function testDefaultEagerConstraintsWhenIncrementing()
9797
$relation->addEagerConstraints($models);
9898
}
9999

100+
public function testDefaultEagerConstraintsWhenIncrementingAndNonIntKeyType()
101+
{
102+
$relation = $this->getRelation(null, false, 'string');
103+
$relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', m::mustBe([null]));
104+
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
105+
$relation->addEagerConstraints($models);
106+
}
107+
100108
public function testDefaultEagerConstraintsWhenNotIncrementing()
101109
{
102110
$relation = $this->getRelation(null, false);
@@ -105,12 +113,13 @@ public function testDefaultEagerConstraintsWhenNotIncrementing()
105113
$relation->addEagerConstraints($models);
106114
}
107115

108-
protected function getRelation($parent = null, $incrementing = true)
116+
protected function getRelation($parent = null, $incrementing = true, $keyType = 'int')
109117
{
110118
$builder = m::mock('Illuminate\Database\Eloquent\Builder');
111119
$builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
112120
$related = m::mock('Illuminate\Database\Eloquent\Model');
113121
$related->incrementing = $incrementing;
122+
$related->shouldReceive('getKeyType')->andReturn($keyType);
114123
$related->shouldReceive('getIncrementing')->andReturn($incrementing);
115124
$related->shouldReceive('getKeyName')->andReturn('id');
116125
$related->shouldReceive('getTable')->andReturn('relation');

0 commit comments

Comments
 (0)