PHP code example of djvibegga / yii2-nested-set-behavior
1. Go to this page and download the library: Download djvibegga/yii2-nested-set-behavior 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/ */
djvibegga / yii2-nested-set-behavior example snippets
class Category extends ActiveRecord
{
public function behaviors() {
return [
[
'class' => NestedSet::className(),
],
];
}
public static function createQuery()
{
return new CategoryQuery(['modelClass' => get_called_class()]);
}
}
class CategoryQuery extends ActiveQuery
{
public function behaviors() {
return [
[
'class' => NestedSetQuery::className(),
],
];
}
}
$roots = Category::find()->roots()->all();
$category = Category::find(1);
$descendants = $category->descendants()->all();
$category = Category::find(1);
$descendants = $category->children()->all();
$category = Category::find(5);
$ancestors = $category->ancestors()->all();
$category = Category::find(9);
$parent = $category->parent()->one();
$category = Category::find(9);
$nextSibling = $category->next()->one();
Category::find()->addOrderBy('lft')->all();
Category::find()->where('root = ?', [$root_id])->addOrderBy('lft')->all();
$root = new Category;
$root->title = 'Mobile Phones';
$root->saveNode();
$root = new Category;
$root->title = 'Cars';
$root->saveNode();
$category1 = new Category;
$category1->title = 'Ford';
$category2 = new Category;
$category2->title = 'Mercedes';
$category3 = new Category;
$category3->title = 'Audi';
$root = Category::find(1);
$category1->appendTo($root);
$category2->insertAfter($category1);
$category3->insertBefore($category1);
$category1 = new Category;
$category1->title = 'Samsung';
$category2 = new Category;
$category2->title = 'Motorola';
$category3 = new Category;
$category3->title = 'iPhone';
$root = Category::find(2);
$category1->appendTo($root);
$category2->insertAfter($category1);
$category3->prependTo($root);
$category1 = new Category;
$category1->title = 'X100';
$category2 = new Category;
$category2->title = 'C200';
$node = Category::find(3);
$category1->appendTo($node);
$category2->prependTo($node);
// move phones to the proper place
$x100 = Category::find(10);
$c200 = Category::find(9);
$samsung = Category::find(7);
$x100->moveAsFirst($samsung);
$c200->moveBefore($x100);
// now move all Samsung phones branch
$mobile_phones = Category::find(1);
$samsung->moveAsFirst($mobile_phones);
// move the rest of phone models
$iphone = Category::find(6);
$iphone->moveAsFirst($mobile_phones);
$motorola = Category::find(8);
$motorola->moveAfter($samsung);
// move car models to appropriate place
$cars = Category::find(2);
$audi = Category::find(3);
$ford = Category::find(4);
$mercedes = Category::find(5);
foreach([$audi, $ford, $mercedes] as $category) {
$category->moveAsLast($cars);
}
$node = Category::find(10);
$node->moveAsRoot();
$root = Category::find(1);
VarDumper::dump($root->isRoot()); //true;
VarDumper::dump($root->isLeaf()); //false;
$node = Category::find(9);
VarDumper::dump($node->isDescendantOf($root)); //true;
VarDumper::dump($node->isRoot()); //false;
VarDumper::dump($node->isLeaf()); //true;
$samsung = Category::find(7);
VarDumper::dump($node->isDescendantOf($samsung)); //true;
$categories = Category::find()->addOrderBy('lft')->all();
$level = 0;
foreach ($categories as $n => $category)
{
if ($category->level == $level) {
echo Html::endTag('li') . "\n";
} elseif ($category->level > $level) {
echo Html::beginTag('ul') . "\n";
} else {
echo Html::endTag('li') . "\n";
for ($i = $level - $category->level; $i; $i--) {
echo Html::endTag('ul') . "\n";
echo Html::endTag('li') . "\n";
}
}
echo Html::beginTag('li');
echo Html::encode($category->title);
$level = $category->level;
}
for ($i = $level; $i; $i--) {
echo Html::endTag('li') . "\n";
echo Html::endTag('ul') . "\n";
}
sh
php composer.phar