eclou/yii2-closure-table

There is no license information available for the latest version (v1.0.2) of this package.

the closure table behavior for yii2

Installs: 22

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

v1.0.2 2024-11-17 17:40 UTC

This package is auto-updated.

Last update: 2025-06-17 18:52:16 UTC


README

Configure

configure query model:

class ClosureQuery extends ActiveQuery
{
    public function behaviors()
    {
        return [
            ClosureTableQueryBehavior::class
        ];
    }
}

configure active model:

class ClosureTable extends ActiveRecord
{
    public function behaviors()
    {
        return [
            [
                'class' => ClosureTableBehavior::class,
                'closureTable' => ClosureTableTree::class, //closure table defined model
                'parentAttribute' => 'parent',
                'childAttribute' => 'child',
                'depthAttribute' => 'depth'
            ]
        ];
    }

    public static function find()
    {
        return new ClosureQuery(static::class);
    }
}

class ClosureTableTree extends ActiveRecord
{
    public static function tableName()
    {
        return 'closure_table_tree';
    }
    
    public function rules()
    {
        return [
            [['parent','child','depth'],'integer']
        ];
    }
}

Usage

ClosureTable::findOne(['_id' => 1])->parents()->all()

$node1 = new ClosureTable();
$node1->saveAsRoot();

$node2 = new ClosureTable();
$node2->appenTo($node1);