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

Honor primary_key option in $belongs_to and $has_many when eager loading #578

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

xak2000
Copy link

@xak2000 xak2000 commented Jul 27, 2017

Fixes #364, #366.

This pull request has changes equal to #365 and #368, but includes tests to prove the changes works.

Before this change, some eager loaded nested relationships were wiped out
when some first-level relationships were the same model object.

For example, when 2 `Post`s have the same `Author` and you load posts
with option `'include' => ['author' => ['profile']]`, then first loaded
post will have eager loaded author with eager loaded profile, while
the second post will have eager loaded author (cloned), but without
eager loaded profile. `clone` method just doesn't clone the relationships
of cloned author.

This leads to a serious performance degradation. Because you expect all
relationships are eagerly loaded when traverse them in the code, while
in reality only some of them are loaded, and others generate many
unnecessary SQL queries to load them on-the-fly.

After this change, nested relationships will be cloned recursively
instead of wiping them out completely. This leads to expected behavior,
when all eagerly loaded relationships will be preserved and fixes the
performance degradation.
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.

1 participant