2017 © Pedro Peláez
 

yii2-extension yii2-nested-sets

The nested sets behavior for the Yii framework

image

dlds/yii2-nested-sets

The nested sets behavior for the Yii framework

  • Friday, December 22, 2017
  • by dlds
  • Repository
  • 1 Watchers
  • 2 Stars
  • 2,142 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 126 Forks
  • 0 Open issues
  • 16 Versions
  • 5 % Grown

The README.md

Yii 2 Nested Sets Behavior

A modern nested sets behavior for the Yii2 framework utilizing the Modified Preorder Tree Traversal algorithm., (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

$ composer require dlds/yii2-nested-sets

or add, (*4)

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

to the require section of your composer.json file., (*5)

Configuring

Configure model as follows, (*6)

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., (*7)

Configure query class as follows, (*8)

use dlds\nestedsets\NestedSetsQueryBehavior;

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

Usage

Making a root node

To make a root node, (*9)

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

The tree will look like this, (*10)

- Countries

Prepending a node as the first child of another node

To prepend a node as the first child of another node, (*11)

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

The tree will look like this, (*12)

- Countries
    - Russia

Appending a node as the last child of another node

To append a node as the last child of another node, (*13)

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

The tree will look like this, (*14)

- Countries
    - Russia
    - Australia

Inserting a node before another node

To insert a node before another node, (*15)

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

The tree will look like this, (*16)

- Countries
    - Russia
    - New Zeeland
    - Australia

Inserting a node after another node

To insert a node after another node, (*17)

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

The tree will look like this, (*18)

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

Getting the root nodes

To get all the root nodes, (*19)

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

Getting the leaves nodes

To get all the leaves nodes, (*20)

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

To get all the leaves of a node, (*21)

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

Getting children of a node

To get all the children of a node, (*22)

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

To get the first level children of a node, (*23)

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

Getting parents of a node

To get all the parents of a node, (*24)

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

To get the first parent of a node, (*25)

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

The Versions

22/12 2017

dev-master

9999999-dev

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

22/12 2017

1.3.3

1.3.3.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

13/02 2017

1.3.2

1.3.2.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

19/10 2016

1.3.1

1.3.1.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

13/10 2016

1.3.0

1.3.0.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

29/04 2015

1.2.7

1.2.7.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

29/04 2015

1.2.6

1.2.6.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

22/04 2015

1.2.5

1.2.5.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

13/04 2015

1.2.4

1.2.4.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

13/04 2015

1.2.3

1.2.3.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

05/04 2015

1.2.2

1.2.2.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

28/03 2015

1.2.1

1.2.1.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

28/03 2015

1.2

1.2.0.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

02/03 2015

1.1

1.1.0.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

02/03 2015

1.0

1.0.0.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov
by Jiri Svoboda

yii2 nested sets

27/01 2015

0.9.0

0.9.0.0

The nested sets behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexander Kochetov

yii2 nested sets