PHPackages                             marco-kretz/nested-set - 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. [Database &amp; ORM](/categories/database)
4. /
5. marco-kretz/nested-set

ActiveLibrary[Database &amp; ORM](/categories/database)

marco-kretz/nested-set
======================

PHP Implementation of a NestedSet

0263PHP

Since Jul 26Pushed 7y ago1 watchersCompare

[ Source](https://github.com/marco-kretz/php-nested-set)[ Packagist](https://packagist.org/packages/marco-kretz/nested-set)[ RSS](/packages/marco-kretz-nested-set/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

PHP - NestedSet
===============

[](#php---nestedset)

[![Build Status](https://camo.githubusercontent.com/4c6caa7ed9f4221d526cf54807dcfeff365df0ed06bbfb9cd683f4596fa6596a/68747470733a2f2f7472617669732d63692e636f6d2f6d6172636f2d6b7265747a2f7068702d6e65737465642d7365742e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/marco-kretz/php-nested-set)

This is my own implementation of the ["Nested Set Model"](https://en.wikipedia.org/wiki/Nested_set_model).

It has not database connection, yet. Will implement a mysql-synchronization feature soon!

Requirements
------------

[](#requirements)

- composer
- PHP &gt;= 7.1

Node
----

[](#node)

A Node represents a single node (or container) within the NestedSet. It's uniquely identified by a name. It can safely be extended (e.g. by a payload attribute) and will still work with the NestedSet.

There is also an extended `Node` called `PayloadNode` which can hold arbitrary, serializable data.

### Usage

[](#usage)

```
$myNode = new Node('myNode');
$anotherNode = new Node('anotherNode);
```

NestedSet
---------

[](#nestedset)

The heart of this library. It manages a NestedSet-Model and uses `Node` as nodes (containers). A NestedSet is also an Iterator, so you can simply iterate over it with Nodes as values. Furthermore it's possible to serialize single Nodes or the whole NestedSet.

### Usage

[](#usage-1)

```
$ns = new NestedSet();

// Define nodes
$rootNode = new Node('root');
$childNode1 = new Node('child1');
$childNode2 = new Node('child2');

// Set root node
$ns->addRoot($rootNode);

// Add nodes
$ns->addNode($rootNode, $childNode1);
$ns->addNode($rootNode, $childNode2);

print($ns);

// Remove node
$ns->removeNode($childNode2);

print($ns);

// Retrieve sub-nodes
$rootChildren = $ns->getSubNodes($rootNode);

for ($rootChildren as $rootChild) {
    print($rootChild);
}

// Iterate over NestedSet
for ($ns as $index => $node) {
    print($node);
}

// Serialization
$serialized = serialize($ns);
$nsCopy = unserialize($serialized);

print($ns === $nsCopy); // true
```

Testing
-------

[](#testing)

`composer test` or simply `phpunit`

Todo
----

[](#todo)

- Add database sync
- more?

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1e1468545b16621fa07593c2d0614ffe8fcee16955956f940c9912a55e3bf2a0?d=identicon)[marco-kretz](/maintainers/marco-kretz)

---

Top Contributors

[![marco-kretz](https://avatars.githubusercontent.com/u/2982363?v=4)](https://github.com/marco-kretz "marco-kretz (22 commits)")

### Embed Badge

![Health badge](/badges/marco-kretz-nested-set/health.svg)

```
[![Health](https://phpackages.com/badges/marco-kretz-nested-set/health.svg)](https://phpackages.com/packages/marco-kretz-nested-set)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M543](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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