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] Merge wheres to hasQuery for withCount() #13612

Merged
merged 5 commits into from
May 18, 2016
Merged

[5.2] Merge wheres to hasQuery for withCount() #13612

merged 5 commits into from
May 18, 2016

Conversation

barryvdh
Copy link
Contributor

Fixes missing wheres defined on the relation when creating the subquery for a relation count, see #13414 (comment)

Fixes missing wheres defined on the relation, see #13414 (comment)
@barryvdh
Copy link
Contributor Author

Added tests for this behavior, seems to be good now, if @taylorotwell can verify this is how the mergeModelDefinedRelationWheresToHasQuery is supposed to work :)

@taylorotwell
Copy link
Member

@acasar would be a good one to check this over.

@@ -1030,6 +1030,7 @@ public function withCount($relations)
$relation->getRelated()->newQuery(), $this
);

$this->mergeModelDefinedRelationWheresToHasQuery($query, $relation);
call_user_func($constraints, $query);
Copy link
Contributor

@acasar acasar May 18, 2016

Choose a reason for hiding this comment

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

@barryvdh I think that call_user_func should come before mergeModelDefinedRelationWheresToHasQuery.

That's because you can potentially remove global scopes from the $query in the callback and mergeModelDefinedRelationWheresToHasQuery takes care of removing them from the $relation too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay.
Should I also call toBase() instead of query, to apply the scopes back?

Copy link
Contributor

@acasar acasar May 18, 2016

Choose a reason for hiding this comment

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

Ah, yeah you should. Nice catch!

So, line 1036 instead of $query->getQuery() it should be $query->toBase()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay so I changed both, although I'm not really sure how to write an actual test for that specific case..

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So something like this? 4abb185

@GrahamCampbell GrahamCampbell changed the title Merge wheres to hasQuery for withCount() [5.] Merge wheres to hasQuery for withCount() May 18, 2016
@GrahamCampbell GrahamCampbell changed the title [5.] Merge wheres to hasQuery for withCount() [5.2] Merge wheres to hasQuery for withCount() May 18, 2016
@acasar
Copy link
Contributor

acasar commented May 18, 2016

👍 Looks good!

@JayBizzle
Copy link
Contributor

Good work @barryvdh

Can confirm this has fixed my issue

@GrahamCampbell
Copy link
Member

👍 Good work. ;)

@taylorotwell taylorotwell merged commit 4abb185 into laravel:5.2 May 18, 2016
@barryvdh barryvdh deleted the patch-10 branch May 19, 2016 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants