Skip to content

[5.2] Impliment nested wheres in has queries #13794

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

Merged
merged 1 commit into from
Jun 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
22 changes: 17 additions & 5 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', C
$query = $relation->{$queryType}($relation->getRelated()->newQuery(), $this);

if ($callback) {
call_user_func($callback, $query);
$this->applyCallback($callback, [$query], $query->getQuery());
}

return $this->addHasWhere(
Expand Down Expand Up @@ -1137,14 +1137,27 @@ protected function callScope($scope, $parameters)
{
array_unshift($parameters, $this);

$query = $this->getQuery();
return $this->applyCallback([$this->model, $scope], $parameters);
}

/**
* Apply the given callback to a supplied (or the current) builder instance.
*
* @param callable $callback
* @param array $parameters
* @param \Illuminate\Database\Query\Builder $query
* @return mixed
*/
protected function applyCallback(callable $callback, $parameters = [], $query = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this method? Could it have a better more descriptive name than applyCallback which sounds very vague and general?

Copy link
Contributor

@acasar acasar Jun 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It applies the callback in a way that doesn't cause any logical issues with boolean order.

To put it simply - "or where" conditions are nested. And since this is now needed in two different places, it was extracted in its own method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like applyCallbackAndNestWheres?

{
$query = $query ?: $this->getQuery();

// We will keep track of how many wheres are on the query before running the
// scope so that we can properly group the added scope constraints in the
// query as their own isolated nested where statement and avoid issues.
$originalWhereCount = count($query->wheres);

$result = call_user_func_array([$this->model, $scope], $parameters) ?: $this;
$result = call_user_func_array($callback, $parameters) ?: $this;

if ($this->shouldNestWheresForScope($query, $originalWhereCount)) {
$this->nestWheresForScope($query, $originalWhereCount);
Expand Down Expand Up @@ -1238,8 +1251,7 @@ protected function nestWheresForScope(QueryBuilder $query, $whereCounts)
// We will construct where offsets by adding the outer most offsets to the
// collection (0 and total where count) while also flattening the array
// and extracting unique values, ensuring that all wheres are sliced.
$whereOffsets = collect([0, $whereCounts, count($allWheres)])
->flatten()->unique();
$whereOffsets = collect([0, $whereCounts, count($allWheres)])->flatten()->unique();

$sliceFrom = $whereOffsets->shift();

Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,21 @@ public function testHasWithContraintsAndHavingInSubquery()
$this->assertEquals(['baz', 'qux', 'quuux'], $builder->getBindings());
}

public function testHasWithContraintsWithOrWhereAndHavingInSubquery()
{
$model = new EloquentBuilderTestModelParentStub;

$builder = $model->where('name', 'larry');
$builder->whereHas('address', function ($q) {
$q->where('zipcode', '90210');
$q->orWhere('zipcode', '90220');
$q->having('street', '=', 'fooside dr');
})->where('age', 29);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wleona3 Can you maybe use some more "human readable" example? like users, posts, etc. instead of quuux and baaam...

$this->assertEquals('select * from "eloquent_builder_test_model_parent_stubs" where "name" = ? and exists (select * from "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" and ("zipcode" = ? or "zipcode" = ?) having "street" = ?) and "age" = ?', $builder->toSql());
$this->assertEquals(['larry', '90210', '90220', 'fooside dr', 29], $builder->getBindings());
}

public function testHasWithContraintsAndJoinAndHavingInSubquery()
{
$model = new EloquentBuilderTestModelParentStub;
Expand Down Expand Up @@ -711,6 +726,11 @@ public function foo()
return $this->belongsTo('EloquentBuilderTestModelCloseRelatedStub');
}

public function address()
{
return $this->belongsTo('EloquentBuilderTestModelCloseRelatedStub', 'foo_id');
}

public function activeFoo()
{
return $this->belongsTo('EloquentBuilderTestModelCloseRelatedStub', 'foo_id')->where('active', true);
Expand Down