-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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.6] Allow to set opposite relation automatically #22731
[5.6] Allow to set opposite relation automatically #22731
Conversation
You should probably tag this as a work-in-progress since it breaks a lot of existing tests and there's no new test introduced for this functionality. |
@sisve Thanks, good point. I've marked this as work in progress. As I mentioned this is initial version and could be improved if general idea will be accepted to add this to core. |
If that's the point; have you considered a branch in your own repository, and rallying support in #internals on Slack or https://github.com/laravel/internals/? Taylor has recently gone on a 0-open-prs spree, and having one with no activity because you wait for someone to evaluate it, while it breaks stuff, doesn't look good for the longevity of this pr... I believe that this type of functionality has been attempted before, and decline for various reasons. Have you read what happened there? There's no references to those attempts in your post. |
@sisve I haven't found something similar (but maybe there is something like this). I can move it to internals but I believe @taylorotwell need to decide whether something like this is desired to be added to core or not. |
…ions implementing contract
Might be relevant: #22401, #20011, #20053, #6720, #6718
|
@deleugpn Thanks for relevant links. I haven't seen them before but it seems I'm not the only one who would like to have it added to Laravel :) |
It's definitely something I would love to have, but I do feel like it's not easily feasible. Feels like we want an expensive magic. |
As long as it's opt-in, and it's not as expensive as trying to do it yourself (or doing redundant eager loads like 'users.account.user'), I would very much like to see this implemented. |
@taylorotwell What's your opinion about it now? Are you going to merge this or rather not? |
There are still no tests. |
@sisve Yes, but there is no point to write tests if it won't be accepted at all |
Not going to be merged, especially without tests. Sorry. |
If approved this would give 2 benefits:
First example - let's assume we have company and user with 1:1 relationship and run code like this:
As a result we get:
what seems quite strange but this is because $company and $ownerCompany are not the same object.
Now let's assume we have 1 to many relationships (user has multiple companies) and we have code like this:
(inner foreach might be considered as Blade partial). Assuming we have 10 users, each with 20 companies we would execute 202 queries here (200 too much). Of course in above case we could load relationship like this:
but it still produces one not necessary query.
The idea is to add additional
withOpposite
method when defining a relationship which will cause that same model will be reused and no additional queries will be run like so:Now running:
wouldn't execute an additional query and get the result:
as expected.
Also running:
or
or
or
would execute minimum expected queries (in last code we don't use eager loading so it will produce more queries than in previous examples).
In this PR autoloading opposite relationship was introduced only into
HasMany
,BelongsTo
andHasOne
relationships but if approved and makes sense it could be added maybe to some more of them. Also maybe some additional code or tests should be updated.This mechanism is opt-in - without adding
->withOpposite(...)
to the relationship it should work as it's working now.