PHPackages                             sieulog/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. sieulog/yii2-nested-sets

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

sieulog/yii2-nested-sets
========================

The nested sets behavior for the Yii framework

0.9.0(11y ago)038BSD-3-ClausePHP

Since Jan 27Pushed 10y ago1 watchersCompare

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

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

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

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

[![Build Status](https://camo.githubusercontent.com/4c4fbd9b20c41ffd50aea1c2c6026880b2b188a6e16ba08a93c351d9381165df/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6372656f636f6465722f796969322d6e65737465642d736574732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/creocoder/yii2-nested-sets)[![Code Coverage](https://camo.githubusercontent.com/1aa6c63fb2b1061d24837ed72686ae2ae08ab0cc1734d7b764c3be08e362ce4a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6372656f636f6465722f796969322d6e65737465642d736574732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/creocoder/yii2-nested-sets/?branch=master)[![Packagist Version](https://camo.githubusercontent.com/c643c8bd91667ae08eab4733c11d64ccc1ee02f29238c5e076f8d72bdada6420/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6372656f636f6465722f796969322d6e65737465642d736574732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/creocoder/yii2-nested-sets)[![Total Downloads](https://camo.githubusercontent.com/e61849dbfb29a6cac07546b3f56463fa1c27860d6858c4296c63d6aac18c2be5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6372656f636f6465722f796969322d6e65737465642d736574732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/creocoder/yii2-nested-sets)

A modern nested sets behavior for the Yii 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 creocoder/yii2-nested-sets
```

or add

```
"creocoder/yii2-nested-sets": "0.9.*"

```

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

Migrations
----------

[](#migrations)

Run the following command

```
$ yii migrate/create create_menu_table
```

Open the `/path/to/migrations/m_xxxxxx_xxxxxx_create_menu_table.php` file, inside the `up()` method add the following

```
$this->createTable('{{%menu}}', [
    'id' => Schema::TYPE_PK,
    // 'tree' => Schema::TYPE_INTEGER,
    'lft' => Schema::TYPE_INTEGER . ' NOT NULL',
    'rgt' => Schema::TYPE_INTEGER . ' NOT NULL',
    'depth' => Schema::TYPE_INTEGER . ' NOT NULL',
    'name' => Schema::TYPE_STRING . ' NOT NULL',
]);
```

To use multiple tree mode uncomment `tree` field.

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

[](#configuring)

Configure model as follows

```
use creocoder\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 creocoder\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()->roots()->all();
```

### Getting the leaves nodes

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

To get all the leaves nodes

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

To get all the leaves of a node

```
$countries = Menu::findOne(['name' => 'Countries']);
$leaves = $countries->leaves()->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->children()->all();
```

To get the first level children of a node

```
$countries = Menu::findOne(['name' => 'Countries']);
$children = $countries->children(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->parents()->all();
```

To get the first parent of a node

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

Donating
--------

[](#donating)

Support this project and [others by creocoder](https://gratipay.com/creocoder/) via [gratipay](https://gratipay.com/creocoder/).

[![Support via Gratipay](https://camo.githubusercontent.com/1c5285a90da2f0b1ee32a7136cacdc9230404760abc467b90492670975ad5bd6/68747470733a2f2f63646e2e7261776769742e636f6d2f67726174697061792f67726174697061792d62616467652f322e332e302f646973742f67726174697061792e737667)](https://gratipay.com/creocoder/)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.1% 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

4120d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/64c9b7bff910026717f970d45573371debb65a8ff5972260b4475158dddce619?d=identicon)[sieulog](/maintainers/sieulog)

---

Top Contributors

[![creocoder](https://avatars.githubusercontent.com/u/896494?v=4)](https://github.com/creocoder "creocoder (220 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)")[![sieulog](https://avatars.githubusercontent.com/u/14790712?v=4)](https://github.com/sieulog "sieulog (1 commits)")

---

Tags

yii2nested sets

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/sieulog-yii2-nested-sets/health.svg)](https://phpackages.com/packages/sieulog-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)
