PHPackages                             matperez/yii2-materialized-path - 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. [Framework](/categories/framework)
4. /
5. matperez/yii2-materialized-path

ActiveYii2-extension[Framework](/categories/framework)

matperez/yii2-materialized-path
===============================

The materialized path behavior for the Yii framework

1.0.2(10y ago)53.0k3[2 issues](https://github.com/matperez/yii2-materialized-path/issues)BSD-3-ClausePHP

Since Jul 14Pushed 10y ago4 watchersCompare

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

READMEChangelogDependencies (5)Versions (4)Used By (0)

Materialized Path for Yii 2
===========================

[](#materialized-path-for-yii-2)

Materialized Path Tree trait for Yii2 ActiveRecord.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ composer require matperez/yii2-materialized-path
```

or add

```
"matperez/yii2-materialized-path": "*"

```

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

Migrations
----------

[](#migrations)

Run the following command

```
$ yii migrate/create create_tree_table
```

Open the `/path/to/migrations/m_xxxxxx_xxxxxx_create_tree_table.php` file, inside the `up()` method add the following

```
$this->createTable('tree', [
    'id' => Schema::TYPE_PK,
    'name' => Schema::TYPE_STRING.' NOT NULL',
    'path' => Schema::TYPE_STRING.' NOT NULL DEFAULT \'.\'',
    'position' => Schema::TYPE_INTEGER.' NOT NULL DEFAULT 0',
    'level' => Schema::TYPE_INTEGER.' NOT NULL DEFAULT 0',
]);
```

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

[](#configuring)

Configure model as follow:

```
use matperez\mp\MaterializedPathBehavior;
use matperez\mp\MaterializedPathQuery;

class Tree extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => MaterializedPathBehavior::className(),
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['position', 'level'], 'integer'],
            [['path'], 'string', 'max' => 255]
        ];
    }

    /**
     * Query factory
     * @return MaterializedPathQuery
     */
    public static function find()
    {
        return new MaterializedPathQuery(get_called_class());
    }

}
```

Usage
-----

[](#usage)

### Making a root node

[](#making-a-root-node)

To make a root node

```
$root = new Tree(['name' => 'root']);
$root->makeRoot();
```

The tree will look like this

```
- root

```

### Appending a node as the child of another node

[](#appending-a-node-as-the-child-of-another-node)

To prepend a node as the first child of another node

```
$child = new Tree(['name' => 'child']);
$child->appendTo($root);
```

The tree will look like this

```
- root
    - child

```

### Move node up and down among siblings

[](#move-node-up-and-down-among-siblings)

```
$node = Tree::findOne(['name' => 'child']);
// move node up
$node->setPosition($node->position - 1);
// move node down
$node->setPosition($node->position + 1);
```

### Getting the root nodes

[](#getting-the-root-nodes)

To get all the root nodes

```
$roots = Tree::find()->roots()->all();
```

### Getting children of a node

[](#getting-children-of-a-node)

To get all the children of a node

```
$root = Tree::find()->roots()->one();
foreach($root->children as $child) {
    foreach($child->children as $subchild) {
        // do the things with a subchild
    }
}
```

### Getting parents of a node

[](#getting-parents-of-a-node)

To get all the parents of a node

```
$node = Tree::findOne(['name' => 'child']);
$parents = Tree::find()->andWhere(['id' => $node->parentIds])->all();
```

To get the first parent of a node

```
$node = Tree::findOne(['name' => 'child']);
$parent = $node->parent();
```

### Delete node with children

[](#delete-node-with-children)

To delete node with children

```
$node->delete();
```

Testing
-------

[](#testing)

```

    ./vendor/bin/codeception build
    ./vendor/bin/codeception run unit

```

### Todo

[](#todo)

more tests, mode examples

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance12

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~91 days

Total

3

Last Release

3778d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8b2fde2b42281c600f7dd9ee316a323b141294178c65ec60d949d7010ede1fe6?d=identicon)[matperez](/maintainers/matperez)

---

Top Contributors

[![matperez](https://avatars.githubusercontent.com/u/1917115?v=4)](https://github.com/matperez "matperez (3 commits)")[![HaruAtari](https://avatars.githubusercontent.com/u/3523420?v=4)](https://github.com/HaruAtari "HaruAtari (2 commits)")[![sanchezzzhak](https://avatars.githubusercontent.com/u/1337066?v=4)](https://github.com/sanchezzzhak "sanchezzzhak (2 commits)")

---

Tags

yii2materialized path

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/matperez-yii2-materialized-path/health.svg)

```
[![Health](https://phpackages.com/badges/matperez-yii2-materialized-path/health.svg)](https://phpackages.com/packages/matperez-yii2-materialized-path)
```

###  Alternatives

[yiisoft/yii2-twig

The Twig integration for the Yii framework

1431.9M32](/packages/yiisoft-yii2-twig)[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.6k47](/packages/skeeks-cms)[devanych/yii2-cart

Shopping cart for Yii2

2011.2k](/packages/devanych-yii2-cart)[tecnocen/yii2-formgenerator

Yii 2 Library to configure form generator

145.7k](/packages/tecnocen-yii2-formgenerator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
