Resolve nested relations against the innermost ancestor - #1530
Merged
mjauvin merged 1 commit intoAug 26, 2026
Merged
Conversation
resolveNestedContext()'s ambient branch trusted the model handed to initRelation() as the relation's parent. That model is $this->model, set from the ENCLOSING relation, because the call arrives as relationRender() -> validateField() -> initRelation($this->model). At one level of nesting the enclosing record is the parent, so this works. At two it is the grandparent, and the leaf relation resolves against the wrong model - in practice a BadMethodCallException, since the grandparent's class usually has no such relation. Walk the ambient path's hops from the root instead, exactly as the reconstructed branch below already does. Both cases now share one resolution mechanism, and the ambient path gains resolveModelForHops()'s $relation->find($id) parent check at every depth. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
mjauvin
merged commit Aug 26, 2026
416c7a0
into
wintercms:nested-relation-controller
15 checks passed
Member
|
Thanks, will test this. |
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.
Targets the
nested-relation-controllerbranch, notdevelop— a follow-up to #1499 (details in this comment).Problem
resolveNestedContext()'s ambient branch trusts the model passed toinitRelation()as the relation's parent, per its docblock ("$modelis trusted as-is - it's the widget's own bound model"). That model is$this->model, set from the enclosing relation, because the call arrives asrelationRender() → validateField() → initRelation($this->model).At one level of nesting the enclosing record is the parent, so it works. At two it is the grandparent, and the leaf relation resolves against the wrong model:
Reproduced on a real three-level config (
Order → closets → paslatten → closet), where the paslat form's own partial contains a single$this->relationRender('closet')—closetbeing a relation onPaslatten, not onCloset.Fix
Walk the ambient path's hops from the root, exactly as the reconstructed branch two lines below already does:
if ($ambientPath !== null) { $this->nestedField = $ambientPath . '[' . $field . ']'; - return [$model, $field, true]; + [$hops] = $this->parseBracketField($this->nestedField); + + return [$this->resolveModelForHops($this->rootModel, $hops), $field, true]; }Both cases now share one resolution mechanism, and the ambient path gains
resolveModelForHops()'s$relation->find($id)parent check at every depth rather than only on reconstructed paths. The docblock is corrected to match.Tests
Adds
testAmbientFieldTwoLevelsDeepResolvesInnermostAncestorNotEnclosingRecord, built on the branch's existing self-referencingItem → items → Itemfixture. It fails without the change (resolves Item A instead of the sub-item) and passes with it.modules/backendsuite run with and without the change, failures diffed: the only delta is the new test flipping to pass. (43 pre-existing errors inUserTest/UserAuthorizationTestin my environment, unrelated.)