Skip to content

Commit

Permalink
Fix #14743 by considering local key in the relation (#15303)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Sep 6, 2016
1 parent 6a766e9 commit 0bea680
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function addEagerConstraints(array $models)
{
$table = $this->parent->getTable();

$this->query->whereIn($table.'.'.$this->firstKey, $this->getKeys($models));
$this->query->whereIn($table.'.'.$this->firstKey, $this->getKeys($models, $this->localKey));
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/Database/DatabaseEloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ public function testEagerConstraintsAreProperlyAdded()
$relation->addEagerConstraints([$model1, $model2]);
}

public function testEagerConstraintsAreProperlyAddedWithCustomKey()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder');
$builder->shouldReceive('join')->once()->with('users', 'users.id', '=', 'posts.user_id');
$builder->shouldReceive('where')->with('users.country_id', '=', 1);

$country = m::mock('Illuminate\Database\Eloquent\Model');
$country->shouldReceive('getKeyName')->andReturn('id');
$country->shouldReceive('offsetGet')->andReturn(1);
$country->shouldReceive('getForeignKey')->andReturn('country_id');

$user = m::mock('Illuminate\Database\Eloquent\Model');
$user->shouldReceive('getTable')->andReturn('users');
$user->shouldReceive('getQualifiedKeyName')->andReturn('users.id');
$post = m::mock('Illuminate\Database\Eloquent\Model');
$post->shouldReceive('getTable')->andReturn('posts');

$builder->shouldReceive('getModel')->andReturn($post);

$relation = new HasManyThrough($builder, $country, $user, 'country_id', 'user_id', 'not_id');
$relation->getQuery()->shouldReceive('whereIn')->once()->with('users.country_id', [3, 4]);
$model1 = new EloquentHasManyThroughModelStub;
$model1->id = 1;
$model1->not_id = 3;
$model2 = new EloquentHasManyThroughModelStub;
$model2->id = 2;
$model2->not_id = 4;
$relation->addEagerConstraints([$model1, $model2]);
}

public function testModelsAreProperlyMatchedToParents()
{
$relation = $this->getRelation();
Expand Down

0 comments on commit 0bea680

Please sign in to comment.