1
0
Fork 0
mirror of https://github.com/Oreolek/yii2-nested-sets.git synced 2024-06-17 07:10:46 +03:00
Go to file
2015-01-08 23:25:46 +03:00
src Refactored NestedSetsBehavior 2015-01-08 23:19:36 +03:00
tests Refactored unit tests 2015-01-08 23:25:46 +03:00
.gitignore Updated .gitignore 2015-01-02 20:21:37 +03:00
.scrutinizer.yml Added .scrutinizer.yml 2015-01-02 21:08:10 +03:00
.travis.yml Updated .travis.yml 2015-01-02 21:21:59 +03:00
composer.json Moved source files to "src" directory, configure phpunit coverage settings 2015-01-04 13:51:58 +03:00
composer.lock Moved source files to "src" directory, configure phpunit coverage settings 2015-01-04 13:51:58 +03:00
LICENSE.md Updated LICENSE.md 2015-01-02 07:22:45 +03:00
phpunit.xml.dist Moved source files to "src" directory, configure phpunit coverage settings 2015-01-04 13:51:58 +03:00
README.md README.md typo fixed 2015-01-06 01:34:16 +03:00

Nested Sets Behavior for Yii 2

PayPal Donate Build Status Code Coverage

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

Installation

The preferred way to install this extension is through composer.

Either run

$ php composer.phar require creocoder/yii2-nested-sets:dev-master

or add

"creocoder/yii2-nested-sets": "dev-master"

to the require section of your composer.json file.

Configuring

First you need to configure model as follows:

use creocoder\nestedsets\NestedSetsBehavior;

class Tree extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            NestedSetsBehavior::className(),
        ];
    }

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

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

Second you need to configure query model as follows:

use creocoder\nestedsets\NestedSetsQueryBehavior;

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

Usage

Quick Example

Making a root node

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

Make a node as the last child of another node

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

The tree will look like this

- Countries
    - Australia

Make 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
    - Australia

Append 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

Append a node after another node

$argentina = new Menu(['name' => 'Argentina']);
$argentina->insertAfter($australia);

The tree will look like this

- Countries
    - Russia
    - New Zeeland
    - Australia
    - Argentina

Installation

Configuring the model soon to come

BASIC USAGE

Soon to come

ADVANCED USAGE

Soon to come