https://github.com/previousnext/nested-set
PHP Nested Set with Doctrine DBAL storage.
https://github.com/previousnext/nested-set
php-lib
Last synced: 8 months ago
JSON representation
PHP Nested Set with Doctrine DBAL storage.
- Host: GitHub
- URL: https://github.com/previousnext/nested-set
- Owner: previousnext
- Created: 2016-11-20T04:59:24.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2023-05-03T12:26:00.000Z (over 2 years ago)
- Last Synced: 2025-06-13T19:04:05.787Z (8 months ago)
- Topics: php-lib
- Language: PHP
- Homepage:
- Size: 121 KB
- Stars: 15
- Watchers: 11
- Forks: 8
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Nested Set
A PHP Doctrine DBAL implementation for Nested Sets.
[](https://circleci.com/gh/previousnext/nested-set)
## Using
### Create table schema
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').
```php
$schema = new DbalNestedSetSchema($connection, 'my_tree');
schema->create();
```
### Set up the nested set client
Create a new `DbalNestedSet` passing in the DBAL connection and the table name.
```php
$nestedSet = new DbalNestedSet($connection, 'my_tree');
```
### Add a root node
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.
```php
$nodeKey = new NodeKey($id, $revisionId);
$rootNode = $nestedSet->addRootNode($nodeKey);
```
### Add a child node
To add a child node, you provide the parent node, and a child node key.
```php
$nodeKey = new NodeKey($id, $revisionId);
$nestedSet->addNodeBelow($rootNode, $nodeKey);
```
### Find Descendants
To find descendents, you provide the parent node key.
```php
$nodeKey = new NodeKey($id, $revisionId);
$descendants = $this->nestedSet->findDescendants($nodeKey);
```
### Find ancestors
To find ancestors, you provide the child node key.
```php
$nodeKey = new NodeKey($id, $revisionId);
$ancestors = $this->nestedSet->findAncestors($nodeKey);
```
See `\PNX\NestedSet\NestedSetInterface` for many more methods that can be used for interacting with the nested set.
## Developing
### Dependencies
To install all dependencies, run:
```
make init
```
### Linting
Uses the Drupal coding standard.
To validate code sniffs run:
```
make lint-php
```
To automatically fix code sniff issues, run:
```
make fix-php
```
### Testing
To run all phpunit tests, run:
```
make test
```