Skip to content

Commit

Permalink
Adds additional check of keyType to avoid using an invalid type for e…
Browse files Browse the repository at this point in the history
…ager load constraints
  • Loading branch information
bryan committed Nov 17, 2016
1 parent d190a63 commit c803376
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,16 @@ public function getIncrementing()
return $this->incrementing;
}

/**
* Get the value indicating the auto incrementing key type.
*
* @return string
*/
public function getKeyType()
{
return $this->keyType;
}

/**
* Set whether IDs are incrementing.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected function getEagerModelKeys(array $models)
// null or 0 in (depending on if incrementing keys are in use) so the query wont
// fail plus returns zero results, which should be what the developer expects.
if (count($keys) === 0) {
return [$this->related->getIncrementing() ? 0 : null];
return [$this->related->getIncrementing() && $this->related->getKeyType() === 'int' ? 0 : null];
}

return array_values(array_unique($keys));
Expand Down
11 changes: 10 additions & 1 deletion tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ public function testDefaultEagerConstraintsWhenIncrementing()
$relation->addEagerConstraints($models);
}

public function testDefaultEagerConstraintsWhenIncrementingAndNonIntKeyType()
{
$relation = $this->getRelation(null, false, 'string');
$relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', m::mustBe([null]));
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}

public function testDefaultEagerConstraintsWhenNotIncrementing()
{
$relation = $this->getRelation(null, false);
Expand All @@ -105,12 +113,13 @@ public function testDefaultEagerConstraintsWhenNotIncrementing()
$relation->addEagerConstraints($models);
}

protected function getRelation($parent = null, $incrementing = true)
protected function getRelation($parent = null, $incrementing = true, $keyType = 'int')
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder');
$builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
$related = m::mock('Illuminate\Database\Eloquent\Model');
$related->incrementing = $incrementing;
$related->shouldReceive('getKeyType')->andReturn($keyType);
$related->shouldReceive('getIncrementing')->andReturn($incrementing);
$related->shouldReceive('getKeyName')->andReturn('id');
$related->shouldReceive('getTable')->andReturn('relation');
Expand Down

0 comments on commit c803376

Please sign in to comment.