Fix relation override scope lost when Select is cloned#569
Merged
Conversation
A relation `load()` override scope (`scope: false` or a custom QueryScope) is silently dropped when the Select is cloned. fetchOne()/findOne() clone internally via `(clone $this)->limit(1)`, so the override never reaches the executed query and the relation's source scope is wrongly re-applied. These tests are currently red and pin the expected behavior: - testDisabledRelationScopeSurvivesClone - testFetchOneKeepsDisabledRelationScope - testFetchOneKeepsCustomRelationScopeOverride
JoinableLoader::withContext() decided the relation scope from the presence of the `scope` key in the *passed* `$options` argument. During a clone the loader tree is re-parented via AbstractLoader::__clone(), which calls withContext($this) with no options, so the code always fell into the `else` branch and re-applied the source scope — silently discarding any override set through load(..., scope: false) or load(..., scope: new QueryScope(...)). Because fetchOne()/findOne() clone internally ((clone $this)->limit(1)), and addGroupByPK() clones for paginated joined queries, the override never reached the executed query. fetchAll() on a POSTLOAD relation does not clone, which is why the bug escaped the existing tests. Drive the scope decision off the persisted `options['scope']` instead. That option survives cloning via the `$options + $this->options` merge, so the override (false / null / ScopeInterface / string) is now preserved across clones, while an absent key or `true` still resolves to the source scope.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 2.x #569 +/- ##
============================================
+ Coverage 91.61% 91.63% +0.02%
+ Complexity 2017 2014 -3
============================================
Files 131 131
Lines 5234 5235 +1
============================================
+ Hits 4795 4797 +2
+ Misses 439 438 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔍 What was changed
JoinableLoader::withContext() decided the relation scope from the presence
of the
scopekey in the passed$optionsargument. During a clone theloader tree is re-parented via AbstractLoader::__clone(), which calls
withContext($this) with no options, so the code always fell into the
elsebranch and re-applied the source scope — silently discarding any override
set through load(..., scope: false) or load(..., scope: new QueryScope(...)).
Because fetchOne()/findOne() clone internally ((clone $this)->limit(1)), and
addGroupByPK() clones for paginated joined queries, the override never
reached the executed query. fetchAll() on a POSTLOAD relation does not clone,
which is why the bug escaped the existing tests.
Drive the scope decision off the persisted
options['scope']instead. Thatoption survives cloning via the
$options + $this->optionsmerge, so theoverride (false / null / ScopeInterface / string) is now preserved across
clones, while an absent key or
truestill resolves to the source scope.