PHPackages                             ben-nsng/nestedset-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ben-nsng/nestedset-php

ActiveLibrary

ben-nsng/nestedset-php
======================

A PDO plugin implementing the nested set pattern

v0.0.1(10y ago)238.2k↓43.3%10[2 issues](https://github.com/ben-nsng/nestedset-php/issues)MITPHP

Since Jun 24Pushed 10y ago6 watchersCompare

[ Source](https://github.com/ben-nsng/nestedset-php)[ Packagist](https://packagist.org/packages/ben-nsng/nestedset-php)[ RSS](/packages/ben-nsng-nestedset-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

nestedset-php
=============

[](#nestedset-php)

If you are not using any PHP framework and need to manage tree structure data, here is the library using Nested Sets Pattern to Model Tree Structure in PHP

### Installation

[](#installation)

To install the library, you can simply clone the library and include the file pdo.nestedset.php. If you are using [Composer](http://getcomposer.org/)in your project, it is really simple to install nestedset-php.

```
# Install Composer
curl -sS https://getcomposer.org/installer | php

# Add nestedset-php as a dependency
php composer.phar require ben-nsng/nestedset-php:*
```

### Usage

[](#usage)

```
$pdo = new PDO('mysql:host=localhost;dbname=tree', 'tree', 'tree');
$treeModel = new NestedSet($pdo);
$treeModel->changeTable('tree'); // default table is tree
```

### API

[](#api)

#### Instance methods:

[](#instance-methods)

```
// init table for first creating table
$treeModel->addRoot();

// add new node to root node, return node id
$treeModel->addNode($label);

// add new node into parent node return node id
$treeModel->addNode($label, $parent_id);

// return database statement object
$nodes = $treeModel->selectAll();
// array of nodes (stdclass)
$nodes->result();

// move existing node into parent node
$treeModel->addChild($node_id, $parent_id);

// move existing node before next node
$treeModel->addBefore($node_id, $next_id);

// move existing node after last node
$treeModel->addBefore($node_id, $last_id);

// delete existing node, including nodes inside node
$treeModel->deleteNode($node_id)
```

### Related Links/Resources

[](#related-linksresources)

- Inspired by \[MySQL stored procedures to manage hierarchical trees in a MySQL database\] ()
- \[Nested Set Model\] ([http://en.wikipedia.org/wiki/Nested\_set\_model](http://en.wikipedia.org/wiki/Nested_set_model))
- \[Managing Hierarchical Data in MySQL\] ()
- \[Storing Hierarchical Data in a Database\] ()

### Authors

[](#authors)

- Ben Ng:

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3982d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ac288317437aa24551e3021f3a4c34f4e00ce728622b886c348ca21ef06c257a?d=identicon)[ben-nsng](/maintainers/ben-nsng)

---

Top Contributors

[![ben-nsng](https://avatars.githubusercontent.com/u/5063993?v=4)](https://github.com/ben-nsng "ben-nsng (9 commits)")

### Embed Badge

![Health badge](/badges/ben-nsng-nestedset-php/health.svg)

```
[![Health](https://phpackages.com/badges/ben-nsng-nestedset-php/health.svg)](https://phpackages.com/packages/ben-nsng-nestedset-php)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
