2017 © Pedro PelĂĄez
 

yii2-extension yii2-nested-set-behavior

This extension allows you to get functional for nested set trees.

image

wbraganca/yii2-nested-set-behavior

This extension allows you to get functional for nested set trees.

  • Monday, December 1, 2014
  • by wbraganca
  • Repository
  • 5 Watchers
  • 19 Stars
  • 1,256 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 14 Forks
  • 5 Open issues
  • 1 Versions
  • 2 % Grown

The README.md

Nested Set behavior for Yii 2

This extension allows you to get functional for nested set trees., (*1)

Installation

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

Either run, (*3)

php composer.phar require wbraganca/yii2-nested-set-behavior "*"

or add, (*4)

"wbraganca/yii2-nested-set-behavior": "*"

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

Configuring

First you need to configure model as follows:, (*6)

use wbraganca\behaviors\NestedSetBehavior;
use wbraganca\behaviors\NestedSetQuery; 

class Category extends ActiveRecord
{
    public function behaviors()
    {
        return [
            [
                'class' => NestedSetBehavior::className(),
                // 'rootAttribute' => 'root',
                // 'levelAttribute' => 'level',
                // 'hasManyRoots' => true
            ],
        ];
    }

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

There is no need to validate fields specified in leftAttribute, rightAttribute, rootAttribute and levelAttribute options. Moreover, there could be problems if there are validation rules for these. Please check if there are no rules for fields mentioned in model's rules() method., (*7)

In case of storing a single tree per database, DB structure can be built with schema/schema.sql. If you're going to store multiple trees you'll need schema/schema-many-roots.sql., (*8)

By default leftAttribute, rightAttribute and levelAttribute values are matching field names in default DB schemas so you can skip configuring these., (*9)

There are two ways this behavior can work: one tree per table and multiple trees per table. The mode is selected based on the value of hasManyRoots option that is false by default meaning single tree mode. In multiple trees mode you can set rootAttribute option to match existing field in the table storing the tree., (*10)

Selecting from a tree

In the following we'll use an example model Category with the following in its DB:, (*11)

- 1. Mobile phones
    - 2. iPhone
    - 3. Samsung
        - 4. X100
        - 5. C200
    - 6. Motorola
- 7. Cars
    - 8. Audi
    - 9. Ford
    - 10. Mercedes

In this example we have two trees. Tree roots are ones with ID=1 and ID=7., (*12)

Getting all roots

$roots = Category::find()->roots()->all();

Result:, (*13)

Array of Active Record objects corresponding to Mobile phones and Cars nodes., (*14)

Getting all descendants of a node

$category = Category::findOne(1);
if ($category) {
    $descendants = $category->descendants()->all();
    var_dump($descendants);
}

Result:, (*15)

Array of Active Record objects corresponding to iPhone, Samsung, X100, C200 and Motorola., (*16)

Getting all children of a node

$category = Category::findOne(1);
if ($category) {
    $descendants = $category->children()->all();
    var_dump($descendants);
}

Result:, (*17)

Array of Active Record objects corresponding to iPhone, Samsung and Motorola., (*18)

Getting all ancestors of a node

$category = Category::findOne(5);
if ($category) {
    $ancestors = $category->ancestors()->all();
    var_dump($ancestors);
}

Result:, (*19)

Array of Active Record objects corresponding to Samsung and Mobile phones., (*20)

Getting parent of a node

$category = Category::findOne(9);
if ($category) {
    $parent = $category->parent()->one();
    var_dump($parent);
}

Result:, (*21)

Array of Active Record objects corresponding to Cars., (*22)

Getting node siblings

Using NestedSet::prev() or NestedSet::next():, (*23)

$category = Category::findOne(9);
if ($category) 
    $nextSibling = $category->next()->one();
}

Result:, (*24)

Array of Active Record objects corresponding to Mercedes., (*25)

Getting the whole tree

You can get the whole tree using standard AR methods like the following., (*26)

For single tree per table:, (*27)

Category::find()->addOrderBy('lft')->all();

For multiple trees per table:, (*28)

Category::find()->andWhere('root = ?', [$root_id])->addOrderBy('lft')->all();

Modifying a tree

In this section we'll build a tree like the one used in the previous section., (*29)

Creating root nodes

You can create a root node using NestedSet::saveNode()., (*30)

$root = new Category;
$root->title = 'Mobile Phones';
$root->saveNode();

$root = new Category;
$root->title = 'Cars';
$root->saveNode();

Result:, (*31)

- 1. Mobile Phones
- 2. Cars

Adding child nodes

There are multiple methods allowing you adding child nodes. To get more info about these refer to API. Let's use these to add nodes to the tree we have:, (*32)

$category1 = new Category;
$category1->title = 'Ford';

$category2 = new Category;
$category2->title = 'Mercedes';

$category3 = new Category;
$category3->title = 'Audi';

$root = Category::findOne(1);
$category1->appendTo($root);
$category2->insertAfter($category1);
$category3->insertBefore($category1);

Result:, (*33)

- 1. Mobile phones
    - 3. Audi
    - 4. Ford
    - 5. Mercedes
- 2. Cars

Logically the tree above doesn't looks correct. We'll fix it later., (*34)

$category1 = new Category;
$category1->title = 'Samsung';

$category2 = new Category;
$category2->title = 'Motorola';

$category3 = new Category;
$category3->title = 'iPhone';

$root = Category::findOne(2);
$category1->appendTo($root);
$category2->insertAfter($category1);
$category3->prependTo($root);

Result:, (*35)

- 1. Mobile phones
    - 3. Audi
    - 4. Ford
    - 5. Mercedes
- 2. Cars
    - 6. iPhone
    - 7. Samsung
    - 8. Motorola
$category1 = new Category;
$category1->title = 'X100';

$category2 = new Category;
$category2->title = 'C200';

$node = Category::findOne(3);
$category1->appendTo($node);
$category2->prependTo($node);

Result:, (*36)

- 1. Mobile phones
    - 3. Audi
        - 9. ĐĄ200
        - 10. X100
    - 4. Ford
    - 5. Mercedes
- 2. Cars
    - 6. iPhone
    - 7. Samsung
    - 8. Motorola

Moving a node making it a new root

There is a special moveAsRoot() method that allows moving a node and making it a new root. All descendants are moved as well in this case., (*37)

Example:, (*38)

$node = Category::findOne(10);
$node->moveAsRoot();

Recursive tree traversal

Category::find()->options();     // List all the tree
Category::find()->options(1);    // List all category in tree with root.id=1
Category::find()->options(1, 3); // List 3 levels of category in tree with root.id=1

Data format for Fancytree.

Category::find()->dataFancytree();     // List all the tree
Category::find()->dataFancytree(1);    // List all category in tree with root.id=1
Category::find()->dataFancytree(1, 3); // List 3 levels of category in tree with root.id=1

The Versions

01/12 2014

dev-master

9999999-dev https://github.com/wbraganca/yii2-nested-set-behavior

This extension allows you to get functional for nested set trees.

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexander Kochetov

extension yii2 nested set yii2 behavior yii2-nested-set-behavior