Fix/select tree modify query subtree#212
Conversation
|
Hey @Baspa I found some weird behaviour with That second part meant the “children” query could still pull basically everything, so you’d get weird extra categories in the tree even when you thought you filtered it. I put up a PR that keeps two queries but, when modifyQueryUsing is set, figures out the subtree from the matching roots and sticks a whereIn on both queries so it only loads that branch. Can you check if that would mess with anything you’re doing on your side? |
|
@CodeWithDennis Thanks for reaching out! I will try to check tomorrow if it does. Will let you know. |
|
One concern:
Could |
|
Mmm.. its a pretty complex problem and i don't wanna break other peoples stuff either.. like the original idea was simple right; you have a root with children, you can filter the roots but still see the children, and if you wanna filter out children you use the child query. But so much code has changed that im honestly not even sure whats the best way to do it without breaking things i just pushed another change, do you think that could work? Im open to suggestions and changes too its genuinely a hard one. |
…eWithDennis/filament-select-tree into fix/select-tree-modify-query-subtree
|
I am reminded of #178 and #179 The original issue was that filtering root items would not yield children that were present in the results but did not have a "null" In the interest of not introducing breaking changes, I have a suggestion:
I believe it should be introduced as optional as to not regress #178 . On a side note, I think that a
Again, such changes are best saved for a major version, but I think it would make sense to change the I have other minor suggestions for a future major version, such as a couple poorly named methods, but I'll save those for later with the hope I'll get a chance to voice them before it releases. |
|
@gp-lnuff I like this idea! |
Summary
SelectTreebuilds options from two Eloquent queries: one for root rows (parent_idis null) and one for non-root rows. Previously,modifyQueryUsingonly applied to the root query. The non-root query still loaded all related records with a parent, so unrelated branches could appear andbuildTreeFromResultscould place nodes incorrectly.This PR, when
modifyQueryUsingis present, resolves matching root primary keys, walks descendants via theparent_idgraph, and constrains both queries withwhereInon that id set so only the intended subtree is loaded. Behavior withoutmodifyQueryUsingis unchanged from4.x.Example usage
Limit the picker to the category branch rooted at
id = 1, including all descendants:The fourth argument scopes which roots participate. The fifth runs only on the non-root query;
return $queryadds no extra constraints.Previous behavior (technical)
Query A — roots
Query B — non-roots
With
where('id', 1)on A only, A returned the single root, while B returned every non-root row in the relation. Descendants of that root appeared because they were included in B, but so could any other non-root row.Timeline
7e57336: Two-query layout; modifier on roots only → subtree intent leaked via unscoped B.7e57336(Baspa): Single full query with the modifier on the entire builder →where('id', 1)returned one row; descendants required an explicit wider predicate.31efbc6(revert): Restored two-query / root-only modifier → leak returned.Risks
modifyQueryUsingare not automatically re-run on each descendant fetch; use model global scopes or a shared base query onSelectTreewhen a predicate must apply to every row.No
modifyQueryUsingIf the fourth argument is omitted, queries match current
4.x(full relation, split roots / non-roots).