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.2] Return null instead of 0 for a default BelongsTo key #13378

Merged
merged 3 commits into from
May 1, 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: 5 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ protected function getEagerModelKeys(array $models)
}
}

// If there are no keys that were not null we will just return an array with 0 in
// it so the query doesn't fail, but will not return any results, which should
// be what this developer is expecting in a case where this happens to them.
if (count($keys) == 0) {
return [0];
// If there are no keys that were not null we will just return an array with either
// null or 0 in (depending on if incrementing keys are in use) so the query wont
// fail and returns no results, which should be what the developer expects.
if (count($keys) === 0) {
return [$this->related->incrementing ? 0 : null];
}

return array_values(array_unique($keys));
Expand Down
24 changes: 23 additions & 1 deletion tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,28 @@ public function testAssociateMethodSetsForeignKeyOnModelById()
$relation->associate(1);
}

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

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

protected function getRelation($parent = null, $incrementing = true)
{
$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('getKeyName')->andReturn('id');
$related->shouldReceive('getTable')->andReturn('relation');
$builder->shouldReceive('getModel')->andReturn($related);
Expand All @@ -112,3 +129,8 @@ class AnotherEloquentBelongsToModelStub extends Illuminate\Database\Eloquent\Mod
{
public $foreign_key = 'foreign.value.two';
}

class MissingEloquentBelongsToModelStub extends Illuminate\Database\Eloquent\Model
{
public $foreign_key = null;
}