PHPackages                             ghopper/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. ghopper/nested\_set

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

ghopper/nested\_set
===================

Tiny class which provides basic functions for manipulation with 'Nested Set' database tree.

v0.3.6(9y ago)147MITPHP

Since Feb 22Pushed 9y ago1 watchersCompare

[ Source](https://github.com/GHopperMSK/nested_set)[ Packagist](https://packagist.org/packages/ghopper/nested_set)[ RSS](/packages/ghopper-nested-set/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (2)Versions (10)Used By (0)

nested\_set
===========

[](#nested_set)

Tiny class which provides basic functions for manipulating with "Nested Set" database tree.

Preparing
---------

[](#preparing)

Create the database table

```
DROP TABLE IF EXISTS ns_tree;
CREATE TABLE ns_tree (
    id INT AUTO_INCREMENT PRIMARY KEY,
    lft INT NOT NULL,
    rgt INT NOT NULL,
    -- you can add whatever you want with text type
    -- such as 'name','description','link' etc
    name VARCHAR(255),
    link VARCHAR(255)
);
INSERT INTO ns_tree (lft,rgt,name,link) VALUES(1,2,'root','root_link');

```

Setup PDO-connection. The class sets PDO attribute PDO::ATTR\_ERRMODE equal to PDO::ERRMODE\_EXCEPTION if it isn't set yet

```
...
$pdo = new \PDO(DB_CONN_STRING, DB_USER, DB_PASS,
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);

```

Configuring
-----------

[](#configuring)

```
$aConfig = array(
    'tb_name' => 'ns_tree',
    'tb_field_index'=> 'id',
    'tb_field_left' => 'lft',
    'tb_field_right' => 'rgt',
    'tb_extra_fields'=> array(
        'name',
        'link'
    )
);

```

Usage
-----

[](#usage)

### create the object

[](#create-the-object)

```
$ns = new CNestedSet($pdo, $aConfig);

```

### add new node

[](#add-new-node)

Pass array with extra fields' values

```
$aNewNode = array(
    'name' => 'new node name',
    'link' => 'new node link'
);
$nd1 = $ns->addChild(1, $aNewNode);

```

### print the tree

[](#print-the-tree)

Returns array with 'depth' values.

```
print_r($ns->getTree($nd1), TRUE);

```

Look at the *examples* folder for more information.

Available methods
-----------------

[](#available-methods)

- getTree($parent\_id) - return all descendants of $parent\_id node
- addChild($parent\_id, $values) - add a new node to $parent\_id which contains $values
- addRootChild($values) - add new node to the root element
- moveTree($cur\_parent\_id, $new\_parent\_id) - move the whole tree to a new parent
- deleteTree($node\_id) - delete $node\_id with all descendants
- deleteNode($node\_id) - delete $node\_id only and shift the descendants to level up

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

[](#requirements)

- database has to transactions support
- database has to locks support

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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

Every ~0 days

Total

9

Last Release

3412d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4691144?v=4)[Ian](/maintainers/GHopper)[@ghopper](https://github.com/ghopper)

---

Top Contributors

[![np25071984](https://avatars.githubusercontent.com/u/19670887?v=4)](https://github.com/np25071984 "np25071984 (15 commits)")

---

Tags

nested-set

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[baum/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

2.3k1.6M55](/packages/baum-baum)[rinvex/laravel-categories

Rinvex Categories is a polymorphic Laravel package, for category management. You can categorize any eloquent model with ease, and utilize the power of Nested Sets, and the awesomeness of Sluggable, and Translatable models out of the box.

467165.4k3](/packages/rinvex-laravel-categories)[gazsp/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

130347.4k5](/packages/gazsp-baum)[typicms/nestablecollection

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

88337.7k24](/packages/typicms-nestablecollection)[baril/bonsai

An implementation of the Closure Tables pattern for Eloquent.

3595.3k](/packages/baril-bonsai)[stefano/stefano-tree

Nested Set(MPTT) implementation for PHP

2790.4k](/packages/stefano-stefano-tree)

PHPackages © 2026

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