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] Fix bindings for has queries #15740

Merged
merged 3 commits into from
Oct 4, 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
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,11 +987,13 @@ public function mergeModelDefinedRelationConstraints(Builder $relation)

$relationQuery = $relation->getQuery();

$whereBindings = Arr::get($relationQuery->getRawBindings(), 'where', []);

// Here we have some relation query and the original relation. We need to copy over any
// where clauses that the developer may have put in the relation definition function.
// We need to remove any global scopes that the developer already removed as well.
return $this->withoutGlobalScopes($removedScopes)->mergeWheres(
$relationQuery->wheres, $relationQuery->getBindings()
$relationQuery->wheres, $whereBindings
);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,21 @@ public function testWhereHasOnNestedSelfReferencingHasManyRelationship()
$this->assertEquals('Grandparent Post', $results->first()->name);
}

public function testHasWithNonWhereBindings()
{
$user = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);

$user->posts()->create(['name' => 'Post 2'])
->photos()->create(['name' => 'photo.jpg']);

$query = EloquentTestUser::has('postWithPhotos');

$bindingsCount = count($query->getBindings());
$questionMarksCount = substr_count($query->toSql(), '?');

$this->assertEquals($questionMarksCount, $bindingsCount);
}

public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverChunkedRequest()
{
$user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
Expand Down Expand Up @@ -971,6 +986,14 @@ public function photos()
{
return $this->morphMany('EloquentTestPhoto', 'imageable');
}

public function postWithPhotos()
{
return $this->post()->join('photo', function ($join) {
$join->on('photo.imageable_id', 'post.id');
$join->where('photo.imageable_type', 'EloquentTestPost');
});
}
}

class EloquentTestUserWithGlobalScope extends EloquentTestUser
Expand Down