How to get a tree in yii2?

category - id , name
category_two - id, category_id, name

Output
- menu
 - menu
 - menu
 - menu
 - menu
 - menu


Tried using https://github.com/paulzi/yii2-adjacency-list

public static function find()
{
 return new SampleQuery(get_called_class());
 }

This function gives the error

How it can be implemented without third-party lib?
June 14th 19 at 18:59
1 answer
June 14th 19 at 19:01
Solution
Extension You cited allows you to manage the storage of wood, not build it from the html list. And it works perfectly.
And Here is the output of the tree can be different, depends on the levels of nesting. The main disadvantage of the principle of "adjacency list" is just a conclusion. If nesting is unknown, it is necessary to write a recursive function, and if level 2, then the usual overkill.
For example generated for the widget menu array with the desired hierarchy.
public static function getMenuList(){
 $root = self::findOne(2); //2 is the id of the root element 
 $leaves = $root->getChildren()->with('children')->all();
 return \yii\helpers\ArrayHelper::toArray(
$leaves,
[
 'common\models\Menu'=>[ //namespace model
'label'=>'name',
 'items'=> function($model){
 return \yii\helpers\ArrayHelper::toArray(
$model->children,
[
'common\models\Menu'=>[
'label'=>'name',
]
]
);
},
]
]
 );

Planned on this extension just to record a video today, will be here tonight:
https://www.youtube.com/channel/UC3jTSXXgSvQI2WJ5f...
And as you can through the controller to create,delete, change the table name? - cayla.Rippin commented on June 14th 19 at 19:04

Find more questions by tags Yii