PHPackages                             serj/sortable-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. serj/sortable-tree

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

serj/sortable-tree
==================

A set of classes for Yii2 to create and maintain a tree.

v1.0.1(7y ago)027MITPHPPHP &gt;=7.0

Since Dec 17Pushed 7y ago1 watchersCompare

[ Source](https://github.com/10111282/yii2-sortable-tree)[ Packagist](https://packagist.org/packages/serj/sortable-tree)[ RSS](/packages/serj-sortable-tree/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

yii2-sortable-tree
==================

[](#yii2-sortable-tree)

A set of classes for Yii2 to create and maintain a tree-like structure.

Installation
------------

[](#installation)

To import the component to your project, put the following line to the require section of your composer.json file:

```
"serj/sortable-tree": "~1.0.0"
```

or run the command

```
$ composer require serj/sortable-tree "~1.0.0"
```

To create database tables apply a migration.

```
./yii migrate --migrationPath=@app/vendor/serj/sortable-tree/migrations
```

Usage
-----

[](#usage)

### Adding a root

[](#adding-a-root)

```
Tree::addItem();
```

### Adding a nested item

[](#adding-a-nested-item)

Assume that root item has an id = 1. To add an item under the root:

```
Tree::addItem(1);
```

Let's add another item, but insert it before previous one. We assume that last inserted item has id = 2.

```
$parent = 1;
$target = 2;
$position = 'before';
Tree::addItem($parent, $target, $position);
```

For the moment, our tree looks like this:

```
├── 1
│   ├── 3
│   └── 2

```

### Moving items

[](#moving-items)

Let's swap items 2 and 3

```
$parent = 1; // we move items under the same parent
$id = 3 // move this item
$target = 2; // we want to locate the item after this one
$position = 'after';
Tree::moveTo($id, $parent, $target, $position)
```

Now it should be like this:

```
├── 1
│   ├── 2
│   └── 3

```

Let's nest the item 3 into the item 2

```
$parent = 2;
$id = 3
$position = 'after';
Tree::moveTo($id, $parent)
```

The result is:

```
├── 1
│   ├── 2
│   	├── 3

```

### Deletion

[](#deletion)

```
Tree::deleteRecursive(2);
```

We deleted item 2 and its child 3. The root item left.

```
├── 1

```

### Getting the tree

[](#getting-the-tree)

```
Tree::getDescendingTree();

```

There are many other methods to work with the tree. For more information you can explore public methods of the *Tree* class and unit tests.

I want to store more data
-------------------------

[](#i-want-to-store-more-data)

Lets suppose you want to store *title*, *created\_at*, *updated\_at* fields. And you no longer want to remove items from the tree, but mark them as *deleted*. To achieve this, we can extend the *Tree* calss. But first, let's modify a migration.

```
class m171217_033811_sortable_tree_tables extends Migration
{
    /**
     * @inheritdoc
     */
    public function safeUp()
    {
        $this->createTable('{{%tree_data}}', [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer(),
            'level' => $this->integer(),
            'sort' => $this->integer(),
            'title' => $this->string(),
            'deleted' => $this->boolean()->defaultValue(false),
            'created_at' => $this->timestamp()->defaultValue('NOW()'),
            'updated_at' => $this->timestamp()->defaultValue('NOW()'),
            'deleted_at' => $this->timestamp(),
        ]);

        $this->createTable('{{%tree_structure}}', [
            'id' => $this->primaryKey(),
            'parent' => $this->integer(),
            'child' => $this->integer()
        ]);
    }

    /**
     * @inheritdoc
     */
    public function safeDown()
    {
        $this->dropTable('{{%tree_data}}');
        $this->dropTable('{{%tree_structure}}');
    }
}
```

Do not forget to apply a new migration.

Extend the tree class. Add and overwrite some methods.

```
