PHP code example of eclou / yii2-closure-table

1. Go to this page and download the library: Download eclou/yii2-closure-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eclou / yii2-closure-table example snippets


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

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']
        ];
    }
}


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

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

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