Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Adds additional check of keyType to avoid using an invalid type for eager load constraints #16452

Merged
merged 1 commit into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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