@@ -2,6 +2,9 @@ This is Laravel 4 package that simplifies creating, managing and retrieving tree
22in database. Using [ Nested Set] ( http://en.wikipedia.org/wiki/Nested_set_model )
33technique high performance descendants retrieval and path-to-node queries can be done.
44
5+ __ IMPORTANT!__ To keep realization of Nested Set Model simple, it's made to work
6+ within single HTTP requests. Don't build trees in your code. [ But, it's possible] ( #multiple-node-insertion ) .
7+
58## Installation
69
710The package can be installed as Composer package, just include it into
@@ -133,6 +136,16 @@ Query can be filtered out from the root node using scope `withoutRoot`:
133136$nodes = Category::withoutRoot()->get();
134137```
135138
139+ Deleting nodes as simple as before:
140+
141+ ``` php
142+ $node->delete();
143+ ```
144+
145+ ### Relations
146+
147+ There are two relations provided by ` Node ` : _ children_ and _ parent_ .
148+
136149### Insertion, re-insertion and deletion of nodes
137150
138151Operations such as insertion and deletion of nodes imply several independent queries
@@ -145,6 +158,55 @@ performance when tree gets update.
145158
146159## Advanced usage
147160
161+ ### Custom collection
162+
163+ This package also provides custom collection, which has two additional functions:
164+ ` toDictionary ` and ` toTree ` . The latter builds a tree from the list of nodes just like
165+ if you would query only root node with all of the children, and children of that
166+ children, etc. This function restores parent-child relations, so the resulting collection
167+ will contain only top-level nodes and each of this node will have ` children ` relation
168+ filled. The interesting thing is that when some node is rejected by a query constraint,
169+ whole subtree will be rejected during building the tree.
170+
171+ Consider the tree of categories:
172+
173+ ```
174+ Catalog
175+ - Mobile
176+ -- Apple
177+ -- Samsung
178+ - Notebooks
179+ -- Netbooks
180+ --- Apple
181+ --- Samsung
182+ -- Ultrabooks
183+ ```
184+
185+ Let's see what we have in PHP:
186+
187+ ``` php
188+ $tree = Category::where('title', '<> ', 'Netbooks')->withoutRoot()->get()->toTree();
189+ echo $tree;
190+ ```
191+
192+ This is what we are going to get:
193+
194+ ``` js
195+ [{
196+ " title" : " Mobile" ,
197+ " children" : [{ " title" : " Apple" , " children" : [] }, { " title" : " Samsung" , " children" : [] }]
198+ },
199+
200+ {
201+ " title" : " Notebooks" ,
202+ " children" : [{ " title" : " Ultrabooks" , " children" : [] }]
203+ }];
204+ ```
205+
206+ Even though the query returned all nodes but _ Netbooks_ , the resulting tree does not contain any
207+ child from that node. This is very helpful when nodes are soft deleted. Active children of soft
208+ deleted nodes will inevitably show up in query results, which is not desired in most situations.
209+
148210### Multiple node insertion
149211
150212_ DO NOT MAKE MULTIPLE INSERTIONS DURING SINGLE HTTP REQUEST_
@@ -179,3 +241,55 @@ work just fine.
179241
180242_ THIS IS THE ONLY CASE WHEN MULTIPLE NODES CAN BE INSERTED AND/OR RE-INSERTED
181243DURING SINGLE HTTP REQUEST WITHOUT REFRESHING DATA_
244+
245+ #### If you still need this
246+
247+ If you are up to create your tree structure in your code, make shure that target node
248+ is always updated. Here is the description of what nodes are target when using insertion
249+ functions:
250+
251+ ``` php
252+ /**
253+ * @var Category $node The node being inserted
254+ * @var Category $target The target node
255+ */
256+
257+ $node->appendTo($target);
258+ $node->prependTo($target);
259+ $node->before($target);
260+ $node->after($target);
261+ $target->append($node);
262+ $target->prepend($node);
263+ ```
264+
265+ When doing multiple insertions, just call ` $target->refresh() ` each time before calling
266+ any of the above functions.
267+
268+ ``` php
269+ DB::transaction(function () {
270+ $node = new Category(...);
271+ $root = Category::root();
272+
273+ // The root here is updated automatically
274+ $node->appendTo($root)->save();
275+
276+ $nodeSubNode = new Category(...);
277+ // No need to update $node since it is just saved
278+ // Also, $node gets update since it is new parent for $nodeSubNode
279+ $nodeSubNode->appendTo($node)->save();
280+
281+ $nodeSibling = new Category(...);
282+ // We refresh $root because it is not updated since last operation
283+ $nodeSibling->appendTo($root->refresh())->save();
284+ });
285+ ```
286+
287+ ### Deleting nodes
288+
289+ To delete a node, you just call ` $node->delete() ` as usual. If node is soft deleted,
290+ nothing happens. But if node is hard deleted, tree updates. But what if this node has
291+ children?
292+
293+ When you create your table's schema and use ` NestedSet::columns ` , it creates foreign
294+ key for you, since nodes are connected by ` parent_id ` attribute. When you hard delete
295+ the node, all of descendants are cascaded.
0 commit comments