PHPackages                             paulzi/yii2-auto-tree - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. paulzi/yii2-auto-tree

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

paulzi/yii2-auto-tree
=====================

Allow apply multiple tree behavior for ActiveRecord in Yii2

v1.1.0(7y ago)5053.8k↓32.4%5[2 issues](https://github.com/paulzi/yii2-auto-tree/issues)5MITPHPPHP &gt;=5.4.0

Since Sep 4Pushed 7y ago8 watchersCompare

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

READMEChangelogDependencies (7)Versions (7)Used By (5)

Yii2 Auto Tree Trait
====================

[](#yii2-auto-tree-trait)

Allow apply multiple tree behavior for ActiveRecord in Yii2.

For selecting methods used by the most rapid behavior. For modifying methods - call all attached behaviors.

If you are using the adjacency list for a get list of the parents or the descendants as relation will be used getParentsOrdered() or getDescendantsOrdered().

[![Packagist Version](https://camo.githubusercontent.com/3b73f0dfba2cdcb4dfc7dc0610efa8e0b39155133a81c210bf8ff5e6e106d151/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7061756c7a692f796969322d6175746f2d747265652e737667)](https://packagist.org/packages/paulzi/yii2-auto-tree)[![Code Coverage](https://camo.githubusercontent.com/ddba71d36f4170068115a4921201cd5baaa20790030b35769be5b3d464b592bd/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f7061756c7a692f796969322d6175746f2d747265652f6d61737465722e737667)](https://scrutinizer-ci.com/g/paulzi/yii2-auto-tree/?branch=master)[![Build Status](https://camo.githubusercontent.com/ed6a51cb48f9604fa154472c5b26881f1d71c34a0bd8738f2ea5615b76d0b94d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7061756c7a692f796969322d6175746f2d747265652f6d61737465722e737667)](https://travis-ci.org/paulzi/yii2-auto-tree)[![Total Downloads](https://camo.githubusercontent.com/6304e3593b3e366206d267ae563e50292d3c95afd76c7ac2f9633d95500b0042/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7061756c7a692f796969322d6175746f2d747265652e737667)](https://packagist.org/packages/paulzi/yii2-auto-tree)

Install
-------

[](#install)

Install via Composer:

```
composer require paulzi/yii2-auto-tree
```

or add

```
"paulzi/yii2-auto-tree" : "^1.1"
```

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

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

[](#configuring)

Install and configure one or more behaviors:

- [Adjacency List](https://github.com/paulzi/yii2-adjacency-list)
- [Nested Sets](https://github.com/paulzi/yii2-nested-sets)
- [Nested Intervals](https://github.com/paulzi/yii2-nested-intervals)
- [Materialized Path](https://github.com/paulzi/yii2-materialized-path)

And use AutoTreeTrait in model.

```
use paulzi\adjacencylist\AdjacencyListBehavior;
use paulzi\nestedsets\NestedSetsBehavior;
use paulzi\autotree\AutoTreeTrait;

class Sample extends \yii\db\ActiveRecord
{
    use AutoTreeTrait;

    public function behaviors() {
        return [
            ['class' => AdjacencyListBehavior::className()],
            ['class' => NestedSetsBehavior::className()],
        ];
    }
}
```

Extending
---------

[](#extending)

If you want to change the order call behavior, override the appropriate methods:

```
use paulzi\autotree\AutoTreeTrait;

class Sample extends \yii\db\ActiveRecord
{
    use AutoTreeTrait;

    /**
     * @param int|null $depth
     * @return \yii\db\ActiveQuery
     */
    public function getParents($depth = null)
    {
        return $this->autoTreeCall('getParents', ['al', 'mp', 'ns', 'ni'], [$depth]);
    }
}
```

If you want to change the list of classes of behaviors:

```
use paulzi\autotree\AutoTreeTrait;

class Sample extends \yii\db\ActiveRecord
{
    use AutoTreeTrait;

    /**
     * @return array
     */
    private static function autoTreeAliases()
    {
        return [
            'example\adjacencylist\AdjacencyListBehavior'       => 'al',
            'example\nestedsets\NestedSetsBehavior'             => 'ns',
            'example\nestedintervals\NestedIntervalsBehavior'   => 'ni',
            'example\materializedpath\MaterializedPathBehavior' => 'mp',
        ];
    }
}
```

Behaviors speed comparison
--------------------------

[](#behaviors-speed-comparison)

```
Name                                            DB Queries  DB Time     Time        Mem

Test 1. Insert. Filling (3 level by 12 child)
    Adjacency List                              5811        1,567 ms    9,591 ms    71.3 MB
    Nested Sets                                 7697        6,672 ms    25,019 ms   105.9 MB
    Nested Intervals amount=24                  5813        1,765 ms    10,442 ms   78.7 MB
    Nested Intervals amount=12 noPrepend noIns. 5813        1,750 ms    10,223 ms   78.7 MB
    Materialized Path (identity pr. key mode)   7696        3,169 ms    13,726 ms   92.5 MB
    Materialized Path (attribute mode)          5811        1,690 ms    9,504 ms    71.6 MB

Test 2. Insert. Filling (6 level by 3 child)
    Adjacency List                              3642        982 ms      5,812 ms    44.5 MB
    Nested Sets                                 4736        5,447 ms    17,859 ms   65.0 MB
    Nested Intervals amount=10                  3644        1,275 ms    5,976 ms    48.9 MB
    Nested Intervals amount=3 noPrepend noIns.  3644        1,271 ms    5,993 ms    48.9 MB
    Materialized Path (identity pr. key mode)   4735        1,316 ms    6,920 ms    57.3 MB
    Materialized Path (attribute mode)          3642        1,129 ms    5,507 ms    44.6 MB

Test 3. Insert in begin 46% 96% (20 in 19657 nodes)
    Adjacency List                              100         31 ms       214 ms      4.5 MB
    Nested Sets                                 100         487 ms      899 ms      4.7 MB
    Nested Intervals                            83          46 ms       187 ms      4.7 MB
    Materialized Path (identity pr. key mode)   120         34 ms       229 ms      4.8 MB
    Materialized Path (attribute mode)          100         470 ms      718 ms      4.6 MB

Test 6. Delete from begin
