2015-01-01 08:08:05 +02:00
|
|
|
# Nested Sets Behavior for Yii 2
|
2013-05-05 19:19:56 +03:00
|
|
|
|
2015-01-01 13:59:38 +02:00
|
|
|
[![PayPal Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WJYG53DVUAALL)
|
2015-01-01 12:58:11 +02:00
|
|
|
[![Build Status](https://img.shields.io/travis/creocoder/yii2-nested-sets/master.svg?style=flat-square)](https://travis-ci.org/creocoder/yii2-nested-sets)
|
2015-01-03 12:12:17 +02:00
|
|
|
[![Code Coverage]https://img.shields.io/scrutinizer/coverage/g/creocoder/yii2-nested-sets/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/creocoder/yii2-nested-sets/?branch=master)
|
2015-01-01 10:04:23 +02:00
|
|
|
|
2015-01-02 09:52:27 +02:00
|
|
|
A modern nested sets behavior for the Yii framework utilizing the Modified Preorder Tree Traversal algorithm.
|
2013-05-06 10:12:45 +03:00
|
|
|
|
2015-01-01 08:08:05 +02:00
|
|
|
## Installation
|
2013-12-30 14:02:19 +02:00
|
|
|
|
|
|
|
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
|
|
|
|
|
|
|
|
Either run
|
|
|
|
|
2015-01-02 06:24:23 +02:00
|
|
|
```bash
|
2015-01-02 09:52:27 +02:00
|
|
|
$ php composer.phar require creocoder/yii2-nested-sets:dev-master
|
2013-12-30 14:02:19 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
or add
|
|
|
|
|
2015-01-02 06:24:23 +02:00
|
|
|
```
|
2015-01-01 08:08:05 +02:00
|
|
|
"creocoder/yii2-nested-sets": "dev-master"
|
2013-12-30 14:02:19 +02:00
|
|
|
```
|
|
|
|
|
2015-01-01 13:59:38 +02:00
|
|
|
to the `require` section of your `composer.json` file.
|
2013-12-30 14:02:19 +02:00
|
|
|
|
2015-01-01 08:08:05 +02:00
|
|
|
## Configuring
|
2013-05-06 10:12:45 +03:00
|
|
|
|
|
|
|
First you need to configure model as follows:
|
|
|
|
|
|
|
|
```php
|
2015-01-01 08:08:05 +02:00
|
|
|
use creocoder\nestedsets\NestedSetsBehavior;
|
2013-05-07 10:32:13 +03:00
|
|
|
|
2015-01-01 08:08:05 +02:00
|
|
|
class Tree extends \yii\db\ActiveRecord
|
2013-05-07 10:32:13 +03:00
|
|
|
{
|
2015-01-01 08:08:05 +02:00
|
|
|
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());
|
|
|
|
}
|
2013-05-06 10:12:45 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-01-01 08:08:05 +02:00
|
|
|
Second you need to configure query model as follows:
|
2013-05-06 10:12:45 +03:00
|
|
|
|
|
|
|
```php
|
2015-01-01 08:08:05 +02:00
|
|
|
use creocoder\nestedsets\NestedSetsQueryBehavior;
|
2013-05-06 10:12:45 +03:00
|
|
|
|
2015-01-01 08:08:05 +02:00
|
|
|
class TreeQuery extends \yii\db\ActiveQuery
|
|
|
|
{
|
|
|
|
public function behaviors() {
|
|
|
|
return [
|
|
|
|
NestedSetsQueryBehavior::className(),
|
|
|
|
];
|
|
|
|
}
|
2013-05-06 10:57:43 +03:00
|
|
|
}
|
2013-05-06 10:12:45 +03:00
|
|
|
```
|
|
|
|
|
2015-01-01 08:08:05 +02:00
|
|
|
## Usage
|
2013-05-06 10:12:45 +03:00
|
|
|
|
2015-01-01 08:08:05 +02:00
|
|
|
TBD.
|