PHPackages                             dlds/yii2-nested-sets - 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. [Framework](/categories/framework)
4. /
5. dlds/yii2-nested-sets

ActiveYii2-extension[Framework](/categories/framework)

dlds/yii2-nested-sets
=====================

The nested sets behavior for the Yii framework

1.3.3(8y ago)23.9k11BSD-3-ClausePHP

Since Jan 27Pushed 8y ago1 watchersCompare

[ Source](https://github.com/dlds/yii2-nested-sets)[ Packagist](https://packagist.org/packages/dlds/yii2-nested-sets)[ RSS](/packages/dlds-yii2-nested-sets/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (16)Used By (1)

Yii 2 Nested Sets Behavior
==========================

[](#yii-2-nested-sets-behavior)

A modern nested sets behavior for the Yii2 framework utilizing the Modified Preorder Tree Traversal algorithm.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ composer require dlds/yii2-nested-sets
```

or add

```
"dlds/yii2-nested-sets": "~1.3.0"

```

to the `require` section of your `composer.json` file.

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

[](#configuring)

Configure model as follows

```
use dlds\nestedsets\NestedSetsBehavior;

class Menu extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            'tree' => [
                'class' => NestedSetsBehavior::className(),
                // 'treeAttribute' => 'tree',
                // 'leftAttribute' => 'lft',
                // 'rightAttribute' => 'rgt',
                // 'depthAttribute' => 'depth',
            ],
        ];
    }

    public function transactions()
    {
        return [
            self::SCENARIO_DEFAULT => self::OP_ALL,
        ];
    }

    public static function find()
    {
        return new MenuQuery(get_called_class());
    }
}
```

To use multiple tree mode uncomment `treeAttribute` array key inside `behaviors()` method.

Configure query class as follows

```
use dlds\nestedsets\NestedSetsQueryBehavior;

class MenuQuery extends \yii\db\ActiveQuery
{
    public function behaviors() {
        return [
            NestedSetsQueryBehavior::className(),
        ];
    }
}
```

Usage
-----

[](#usage)

### Making a root node

[](#making-a-root-node)

To make a root node

```
$countries = new Menu(['name' => 'Countries']);
$countries->makeRoot();
```

The tree will look like this

```
- Countries

```

### Prepending a node as the first child of another node

[](#prepending-a-node-as-the-first-child-of-another-node)

To prepend a node as the first child of another node

```
$russia = new Menu(['name' => 'Russia']);
$russia->prependTo($countries);
```

The tree will look like this

```
- Countries
    - Russia

```

### Appending a node as the last child of another node

[](#appending-a-node-as-the-last-child-of-another-node)

To append a node as the last child of another node

```
$australia = new Menu(['name' => 'Australia']);
$australia->appendTo($countries);
```

The tree will look like this

```
- Countries
    - Russia
    - Australia

```

### Inserting a node before another node

[](#inserting-a-node-before-another-node)

To insert a node before another node

```
$newZeeland = new Menu(['name' => 'New Zeeland']);
$newZeeland->insertBefore($australia);
```

The tree will look like this

```
- Countries
    - Russia
    - New Zeeland
    - Australia

```

### Inserting a node after another node

[](#inserting-a-node-after-another-node)

To insert a node after another node

```
$unitedStates = new Menu(['name' => 'United States']);
$unitedStates->insertAfter($australia);
```

The tree will look like this

```
- Countries
    - Russia
    - New Zeeland
    - Australia
    - United States

```

### Getting the root nodes

[](#getting-the-root-nodes)

To get all the root nodes

```
$roots = Menu::find()->isTreeRoot()->all();
```

### Getting the leaves nodes

[](#getting-the-leaves-nodes)

To get all the leaves nodes

```
$leaves = Menu::find()->isTreeLeaf()->all();
```

To get all the leaves of a node

```
$countries = Menu::findOne(['name' => 'Countries']);
$leaves = $countries->isTreeLeaf()->all();
```

### Getting children of a node

[](#getting-children-of-a-node)

To get all the children of a node

```
$countries = Menu::findOne(['name' => 'Countries']);
$children = $countries->descendants()->all();
```

To get the first level children of a node

```
$countries = Menu::findOne(['name' => 'Countries']);
$children = $countries->descendants(1)->all();
```

### Getting parents of a node

[](#getting-parents-of-a-node)

To get all the parents of a node

```
$countries = Menu::findOne(['name' => 'Countries']);
$parents = $countries->ancestors()->all();
```

To get the first parent of a node

```
$countries = Menu::findOne(['name' => 'Countries']);
$parent = $countries->ancestors(1)->one();
```

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 86.6% 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 ~75 days

Recently: every ~242 days

Total

15

Last Release

3060d ago

Major Versions

0.9.0 → 1.02015-03-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/23f0d05bb48111bd348c97af007c453b374ba03c36ec4fd114dfd73896bf5958?d=identicon)[dlds](/maintainers/dlds)

---

Top Contributors

[![creocoder](https://avatars.githubusercontent.com/u/896494?v=4)](https://github.com/creocoder "creocoder (220 commits)")[![jirisvoboda](https://avatars.githubusercontent.com/u/10264326?v=4)](https://github.com/jirisvoboda "jirisvoboda (16 commits)")[![Mihai-P](https://avatars.githubusercontent.com/u/7947914?v=4)](https://github.com/Mihai-P "Mihai-P (15 commits)")[![zhuravljov](https://avatars.githubusercontent.com/u/1656851?v=4)](https://github.com/zhuravljov "zhuravljov (2 commits)")[![bethrezen](https://avatars.githubusercontent.com/u/260284?v=4)](https://github.com/bethrezen "bethrezen (1 commits)")

---

Tags

yii2nested sets

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dlds-yii2-nested-sets/health.svg)

```
[![Health](https://phpackages.com/badges/dlds-yii2-nested-sets/health.svg)](https://phpackages.com/packages/dlds-yii2-nested-sets)
```

###  Alternatives

[creocoder/yii2-nested-sets

The nested sets behavior for the Yii framework

4454.8M66](/packages/creocoder-yii2-nested-sets)[yiisoft/yii2-twig

The Twig integration for the Yii framework

1431.9M32](/packages/yiisoft-yii2-twig)[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.6k46](/packages/skeeks-cms)[devanych/yii2-cart

Shopping cart for Yii2

2011.2k](/packages/devanych-yii2-cart)[tecnocen/yii2-formgenerator

Yii 2 Library to configure form generator

145.7k](/packages/tecnocen-yii2-formgenerator)

PHPackages © 2026

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