PHP code example of paulzi / yii2-materialized-path
1. Go to this page and download the library: Download paulzi/yii2-materialized-path 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/ */
paulzi / yii2-materialized-path example snippets
class m150828_150000_single_tree extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%single_tree}}', [
'id' => Schema::TYPE_PK,
'path' => Schema::TYPE_STRING . ' NULL',
'depth' => Schema::TYPE_INTEGER . ' NOT NULL',
'sort' => Schema::TYPE_INTEGER . ' NOT NULL',
'name' => Schema::TYPE_STRING . ' NOT NULL', // example field
], $tableOptions);
$this->createIndex('path', '{{%single_tree}}', ['path']);
}
}
class m150828_150100_multiple_tree extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%multiple_tree}}', [
'id' => Schema::TYPE_PK,
'tree' => Schema::TYPE_INTEGER . ' NULL',
'path' => Schema::TYPE_STRING . ' NULL',
'depth' => Schema::TYPE_INTEGER . ' NOT NULL',
'sort' => Schema::TYPE_INTEGER . ' NOT NULL',
'name' => Schema::TYPE_STRING . ' NOT NULL', // example field
], $tableOptions);
$this->createIndex('path', '{{%multiple_tree}}', ['tree', 'path']);
}
}
use paulzi\materializedPath\MaterializedPathBehavior;
class Sample extends \yii\db\ActiveRecord
{
public function behaviors() {
return [
[
'class' => MaterializedPathBehavior::className(),
// 'treeAttribute' => 'tree',
],
];
}
public function transactions()
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
}
class Sample extends \yii\db\ActiveRecord
{
public static function find()
{
return new SampleQuery(get_called_class());
}
}
use paulzi\materializedPath\MaterializedPathQueryTrait;
class SampleQuery extends \yii\db\ActiveQuery
{
use MaterializedPathQueryTrait;
}
$roots = Sample::find()->roots()->all();
$node11 = Sample::findOne(['name' => 'node 1.1']);
$parents = $node11->parents; // via relation
$parents = $node11->getParents()->all(); // via query
$parents = $node11->getParents(2)->all(); // get 2 levels of ancestors
$node11 = Sample::findOne(['name' => 'node 1.1']);
$parent = $node11->parent; // via relation
$parent = $node11->getParent()->one(); // via query
$node11 = Sample::findOne(['name' => 'node 1.1']);
$root = $node11->root; // via relation
$root = $node11->getRoot()->one(); // via query
$node11 = Sample::findOne(['name' => 'node 1.1']);
$descendants = $node11->descendants; // via relation
$descendants = $node11->getDescendants()->all(); // via query
$descendants = $node11->getDescendants(2, true)->all(); // get 2 levels of descendants and self node
$node11 = Sample::findOne(['name' => 'node 1.1']);
$node11->delete(); // delete node, children come up to the parent
$node11->deleteWithChildren(); // delete node and all descendants
$model = Sample::findOne(1);
$model->reorderChildren(true); // reorder with center zero
$model = Sample::findOne(2);
$model->reorderChildren(false); // reorder from zero
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.