|
1 | 1 | # Nested Set
|
2 | 2 |
|
3 |
| -A PHP library for a Nested Set implementation. |
| 3 | +A PHP Doctrine DBAL implementation for Nested Sets. |
4 | 4 |
|
5 | 5 | [](https://circleci.com/gh/previousnext/nested-set)
|
6 | 6 |
|
| 7 | +## Using |
| 8 | + |
| 9 | +### Create table schema |
| 10 | + |
| 11 | +Create the table schema, passing in a a [DBAL connection](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#getting-a-connection) and table name (defaults to 'tree'). |
| 12 | +```php |
| 13 | +$schema = new DbalNestedSetSchema($connection, 'my_tree'); |
| 14 | +schema->create(); |
| 15 | +``` |
| 16 | + |
| 17 | +### Set up the nested set client |
| 18 | + |
| 19 | +Create a new `DbalNestedSet` passing in the DBAL connection and the table name. |
| 20 | + |
| 21 | +```php |
| 22 | +$nestedSet = new DbalNestedSet($connection, 'my_tree'); |
| 23 | +``` |
| 24 | + |
| 25 | +### Add a root node |
| 26 | + |
| 27 | +A NodeKey represents a unique ID for a node in the tree. It supports the idea of a node ID and a revision ID, mostly for compatibility with Drupal. |
| 28 | + |
| 29 | +```php |
| 30 | +$nodeKey = new NodeKey($id, $revisionId); |
| 31 | +$rootNode = $nestedSet->addRootNode($nodeKey); |
| 32 | +``` |
| 33 | + |
| 34 | +### Add a child node |
| 35 | + |
| 36 | +To add a child node, you provide the parent node, and a child node key. |
| 37 | + |
| 38 | +```php |
| 39 | +$nodeKey = new NodeKey($id, $revisionId); |
| 40 | +$nestedSet->addNodeBelow($rootNode, $nodeKey); |
| 41 | +``` |
| 42 | + |
| 43 | +### Find Descendants |
| 44 | + |
| 45 | +To find descendents, you provide the parent node key. |
| 46 | + |
| 47 | +```php |
| 48 | +$nodeKey = new NodeKey($id, $revisionId); |
| 49 | +$descendants = $this->nestedSet->findDescendants($nodeKey); |
| 50 | +``` |
| 51 | + |
| 52 | +### Find ancestors |
| 53 | + |
| 54 | +To find ancestors, you provide the child node key. |
| 55 | + |
| 56 | +```php |
| 57 | +$nodeKey = new NodeKey($id, $revisionId); |
| 58 | +$ancestors = $this->nestedSet->findAncestors($nodeKey); |
| 59 | +``` |
| 60 | + |
| 61 | +See `\PNX\NestedSet\NestedSetInterface` for many more methods that can be used for interacting with the nested set. |
| 62 | + |
7 | 63 | ## Developing
|
8 | 64 |
|
9 | 65 | ### Dependencies
|
|
0 commit comments