1. Go to this page and download the library: Download paulzi/yii2-adjacency-list 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-adjacency-list example snippets
class m150722_150000_adjacency_list 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('{{%adjacency_list}}', [
'id' => Schema::TYPE_PK,
'parent_id' => Schema::TYPE_INTEGER . ' NULL',
'sort' => Schema::TYPE_INTEGER . ' NOT NULL',
'name' => Schema::TYPE_STRING . ' NOT NULL', // example field
], $tableOptions);
$this->createIndex('parent_sort', '{{%adjacency_list}}', ['parent_id', 'sort']);
}
}
use paulzi\adjacencyList\AdjacencyListBehavior;
class Sample extends \yii\db\ActiveRecord
{
public function behaviors() {
return [
[
'class' => AdjacencyListBehavior::className(),
],
];
}
}
class Sample extends \yii\db\ActiveRecord
{
public static function find()
{
return new SampleQuery(get_called_class());
}
}
use paulzi\adjacencyList\AdjacencyListQueryTrait;
class SampleQuery extends \yii\db\ActiveQuery
{
use AdjacencyListQueryTrait;
}
$roots = Sample::find()->roots()->all();
$node11 = Sample::findOne(['name' => 'node 1.1']);
$parents = $node11->parents; // via relation unsorted
$parents = $node11->parentsOrdered; // via relation sorted
$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']);
$ids = $node11->getParentsIds();
$ids = $node11->getParentsIds(3, false); // get 3 levels of ancestors primary keys with force updating from DB
$node11 = Sample::findOne(['name' => 'node 1.1']);
$descendants = $node11->descendants; // via relation unsorted
$descendants = $node11->descendantsOrdered; // via relation sorted
$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']);
$children = $node11->children; // via relation
$children = $node11->getChildren()->all(); // via query
$node11 = Sample::findOne(['name' => 'node 1.1']);
$ids = $node11->getDescendantsIds(); // get array of per-level descendants primary keys
$ids = $node11->getDescendantsIds(null, true); // get flat array of descendants primary keys
$ids = $node11->getDescendantsIds(3, false, false); // get 3 levels array of per-level descendants primary keys with force updating from DB
$node11 = Sample::findOne(['name' => 'node 1.1']);
$leaves = $node11->leaves; // via relation
$leaves = $node11->getLeaves(2)->all(); // get 2 levels of leaves via query
$node11 = Sample::findOne(['name' => 'node 1.1']);
$next = $node11->next; // via relation
$next = $node11->getNext()->one(); // via query
$node11 = Sample::findOne(['name' => 'node 1.1']);
$prev = $node11->prev; // via relation
$prev = $node11->getPrev()->one(); // via query
$node1 = Sample::findOne(['name' => 'node 1']);
$node11 = Sample::findOne(['name' => 'node 1.1']);
$node11->isRoot() - return true, if node is root
$node11->isLeaf() - return true, if node is leaf
$node11->isChildOf($node1) - return true, if node11 is child of $node1
$node11 = new Sample();
$node11->name = 'node 1.1';
$node11->makeRoot()->save();
$node1 = Sample::findOne(['name' => 'node 1']);
$node11 = new Sample();
$node11->name = 'node 1.1';
$node11->prependTo($node1)->save(); // inserting new 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.