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] Consider local key for HasManyThrough #15303

Merged
merged 1 commit into from
Sep 6, 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
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