Skip to content

Commit

Permalink
[5.3] Fix bindings for has queries (#15740)
Browse files Browse the repository at this point in the history
* Fix has bindings

* StyleCI

* Fix id reference
  • Loading branch information
acasar authored and taylorotwell committed Oct 4, 2016
1 parent c6e7d1a commit cc7121a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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

0 comments on commit cc7121a

Please sign in to comment.