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.2] Impliment nested wheres in has queries #13794

Merged
merged 1 commit into from
Jun 1, 2016
Merged

Conversation

wleona3
Copy link
Contributor

@wleona3 wleona3 commented May 31, 2016

Implemented based on #13785. Existing scope methods worked, so they've been renamed and aliased.

@wleona3 wleona3 changed the title Impliment nested wheres in has queries [5.2] Impliment nested wheres in has queries May 31, 2016
* @return bool
*/
protected function shouldNestWheres(QueryBuilder $query, $originalWhereCount)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

@wleona3 I don't think we really need to alias shouldNestWheresForScope. Just use the existing method.

@acasar
Copy link
Contributor

acasar commented May 31, 2016

@wleona3 All in all, I was thinking we could maybe extract the shared logic between callScope and has methods.

I'd suggest something like this: acasar@658663d

@wleona3
Copy link
Contributor Author

wleona3 commented May 31, 2016

Agreed and updated.

@@ -1239,7 +1254,7 @@ protected function nestWheresForScope(QueryBuilder $query, $whereCounts)
// 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();
->flatten()->unique();
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert this line to original formatting.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please revert this change.

@acasar
Copy link
Contributor

acasar commented May 31, 2016

Looking better now :) Can you apply the formatting changes and squash the commits?

@wleona3
Copy link
Contributor Author

wleona3 commented May 31, 2016

Changes made and squashed

@GrahamCampbell
Copy link
Member

One change remaining to be reverted please.

@wleona3
Copy link
Contributor Author

wleona3 commented May 31, 2016

Whoops. Corrected.

@GrahamCampbell
Copy link
Member

Thanks. :)

@acasar
Copy link
Contributor

acasar commented May 31, 2016

@taylorotwell this is now ready for review.

Basically, orWhere constraints in whereHas are now automatically nested, same as with local and global scopes. Example:

Model::whereHas('relation', function ($query) {
    $query->where('foo', 'bar')->orWhere('foo', 'baz');
});

* @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?

@taylorotwell taylorotwell merged commit 0e3486d into laravel:5.2 Jun 1, 2016
@trip-somers
Copy link

Can this be pulled into 5.1?

@GrahamCampbell
Copy link
Member

Can this be pulled into 5.1?

Sorry, no. Our LTS branch gets big fixes only.

@trip-somers
Copy link

You're saying that this -- #13785 (comment) -- isn't considered a bug?

@GrahamCampbell
Copy link
Member

No. It's a feature.

@trip-somers
Copy link

trip-somers commented Jun 10, 2016

orWhere() not nesting properly inside of a whereHas() is a feature? How so?

EDIT: Maybe I'm asking for the wrong commit to be pulled.

@GrahamCampbell
Copy link
Member

You can have joins and other clauses in whereHas so we can't nest everything inside where

@GrahamCampbell
Copy link
Member

The ability to do that is not required, it's a feature.

@taylorotwell
Copy link
Member

No idea what Graham is talking about in regards to "being a feature", so apologize for that... but can you not do this?

->where(function ($q) {
$q->where('foo')->orWhere('bar')
});

@GrahamCampbell
Copy link
Member

@taylorotwell The point is you can achieve the same thing without nesting. The ability to nest these is a feature.

@trip-somers
Copy link

@taylorotwell, I understand what @GrahamCampbell is saying. I see both sides of this discussion. Intuitively, it is weird to have to force the nesting with an extra ->where() closure, but I also understand why auto-nesting it causes other problems in the query builder.

All said, the extra ->where() closure isn't the worst solution.

Thanks for the explanations and suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants