-
Notifications
You must be signed in to change notification settings - Fork 459
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
Add Node::loadDescendants method #48
Conversation
Thanks for contributing! I was reviewing this patch but something popped out: Is this not accomplishable I mean: If you wanted to eager-load the ...
protected $with = array('children');
... You may use the $node->descendants()->with('children')->get(); Or, to load the relation in a particular model which was not eager-loaded $node->load('children'); |
This took me a while to get to, since I'm investigating another baum-related bug. But yes, you're absolutely right. Thanks. |
Hold up -- doesn't |
Calling Of course, the |
Should that load all descendents even if they're several layers deeper? |
Yes it should because Nested Sets work linearly. When you ask for the descendants of a node, you get all of them one after the other (the default sorting is by the |
I want to do what you suggest, but for all roots at the same time. Is there a way to do that? Right now I have |
Well in the case you're showcasing what I said does not work. The descendants queries include the children of each level, so eager-loading works. But in your case, the But, Isn't getting the roots with all their descendants just getting the whole tree? The following queries will produce the desired results returning the full hierarchy in exactly 2 queries: MenuItem::query()->with('children')->get()->toHierarchy(); MenuItem::where(... some scope conditions ...)->with('children')->get()->toHierarchy(); |
I understand that having a collection of root nodes and trying to eager-load all possible nested children is not working right now in your use-case. I sincerely think that your best bet here is to include: I'm soon to start rewriting some bits of this package to allow for trait-based usage and maybe I can re-think/re-do some things for your use-case to work. |
For cases where I already have a Node object and now want to recurse over all
descendants I find this method very useful. It loads all descendants and sets
the children relation so no further database queries need to be made when
looping through children and children's children.
Thanks to @shaddowgh for the help.
This fixes #45.