-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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] Apply constraints to a morphTo relation on eager load #13724
[5.2] Apply constraints to a morphTo relation on eager load #13724
Conversation
6ec9eee
to
eb462b3
Compare
# Conflicts: # src/Illuminate/Database/Eloquent/Relations/MorphTo.php
@@ -186,6 +179,11 @@ protected function getResultsByType($type) | |||
|
|||
$query = $this->useWithTrashed($query); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should remove this line too
👍 This should also fix #13567 |
Do we have tests that fail? |
Nope @taylorotwell. One unit test was removed, but it didn't really add much (I believe all code is still covered through integration tests). |
Are you sure this is a good change? This change breaks the eager loading of my Polymorphic relations. It messes everything up for me. Figuring out why this is happening... |
@markvaneijk Can you paste your code here? ping @phroggyy |
@markvaneijk this should actually fix eager loading of polymorphic relations by allowing constraints, but I might have forgotten about some case, in which case I'll definitely fix it and add a test case. As @acasar said, throw some code in here and we'll do our best. (Would also be great if it's easy-ish to reproduce). |
@phroggy @acasar Thanks for your responses. I tried to make a simple example to reproduce my problem. When you have the Polymorphic relation example in the docs: https://laravel.com/docs/5.2/eloquent-relationships#polymorphic-relations And you only add this, to always eager load the likeable relationship: public $with = ['likeable']; And when I request the Like object: $like = App\Like::find(1); Then I get this error:
When I remove the $with from the Like model, and use it directly when getting the object, it does work: $like = App\Like::with('likeable')->find(1); So the question for me is. Is the use of $with wrong? If not, why does the first example does not work like the second example? |
That's really weird @markvaneijk. Any chance you'd be able to get a sample project up I can clone down? I'll look into the code right now... Hmm, might have something to do with accessing the QB rather than Eloquent Builder somewhere |
@phroggyy, just got the same issue when using a |
@ifox @markvaneijk I'm looking into this right now. |
@acasar so the issue is we're cloning |
@phroggyy Hm.. I think we should actually copy over eager load constraints instead of just resetting them? Because related model can have it's own eager load constraints that we still need to take into account. |
This has broken code in one of my projects. I'm not sure if my use-case is non-supported one, but I have a number of relations set up similarly to this:
Which was used to fetch a polymorphic relation, but include trashed records. This no longer works, instead I'm getting a What would be the approach to load a polymorphic relation including trashed records? |
@dwightwatson Should be fixed on 5.2 dev. Can you check? |
Sure - just tried on Edit: I'll put together an example project that demonstrates the problem, and possibly open a new issue for it. |
@dwightwatson Thanks, example project or failing test would be nice. We are actually testing for this scenario https://github.com/laravel/framework/blob/5.2/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php#L534 So I'd be interested to know the setup in your case. |
@dwightwatson I think I found the issue. Here's the PR #13828 |
Previously, using
Model::load
orwith
and supplying eager load constraints would make no difference if the loaded relationship was of the MorphTo type, as eager load constraints were not applied for this class. This PR fixes that.