Giter Site home page Giter Site logo

yii2-nested-set-behavior's Introduction

Nested Set behavior for Yii 2

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

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

Configuring

First you need to configure model as follows:

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.

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.

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

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.

Selecting from a tree

In the following we'll use an example model Category with the following in its DB:

- 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.

Getting all roots

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

Result:

Array of Active Record objects corresponding to Mobile phones and Cars nodes.

Getting all descendants of a node

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

Result:

Array of Active Record objects corresponding to iPhone, Samsung, X100, C200 and Motorola.

Getting all children of a node

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

Result:

Array of Active Record objects corresponding to iPhone, Samsung and Motorola.

Getting all ancestors of a node

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

Result:

Array of Active Record objects corresponding to Samsung and Mobile phones.

Getting parent of a node

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

Result:

Array of Active Record objects corresponding to Cars.

Getting node siblings

Using NestedSet::prev() or NestedSet::next():

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

Result:

Array of Active Record objects corresponding to Mercedes.

Getting the whole tree

You can get the whole tree using standard AR methods like the following.

For single tree per table:

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

For multiple trees per table:

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.

Creating root nodes

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

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

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

Result:

- 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:

$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:

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

Logically the tree above doesn't looks correct. We'll fix it later.

$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:

- 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:

- 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.

Example:

$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

yii2-nested-set-behavior's People

Contributors

wbraganca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-nested-set-behavior's Issues

Method wbraganca\behaviors\NestedSetQuery::roots() does not exist

`public function behaviors()
{
return [

         'NestedSetBehavior' => [
            'class' => 'wbraganca\behaviors\NestedSetBehavior',
            'attributes' => [
                'rootAttribute' => 'root',
                'levelAttribute' => 'level',
                'hasManyRoots' => true,
            ],
        ],
  ];
}

public static function GetMenu()
{
    $roots = self::find()->roots()->all();

$menu = [];
foreach ($roots as $root) {
$category = self::findOne($root->id);
if ($category) {
$descendants = $category->children()->all();
$menu[]=$descendants;
}
}
}`

Bug with function 'moveAsFirst'

When use this function it sets wrong lft and rgt values. (in example 12, 13 but it must be 10, 11)

'moveAsLast' has the same bug.

P.S
this bug in correctCachedOnMoveNode function - when comment it - everything works good.

20-12-2014 14-20-58

root node

From readme:
README.md#L199
In a single tree per table mode you can create only one root node. If you'll attempt to create more there will be CException thrown.
There no CException is thrown in that case.
And I want a easy approach for node creation, like save() for create not root node at the end of a tree.

Could not install this extension

When I run php composer.phar require wbraganca/yii2-nested-set-behavior "*"
Composer return a error:
The request package wbraganca/yii2-nested-set-behavior could not found in any version, there may be a typo in the package name

Question regarding option "Data format for Fancytree"

Question regarding option "Data format for Fancytree".
Category::find()->dataFancytree() - returns data in Fancytree format. How I can change parameters of items? I want to set these:

    expanded: true, // Initial expanded status.
    selected: true, // Initial selected status.

Is it possible without an additional foreach?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.