Answer the question
In order to leave comments, you need to log in
how to get tree in yii2?
category - id , name
category_two - id, category_id,
name
- menu
- menu
- menu
- menu
- menu
- menu
public static function find()
{
return new SampleQuery(get_called_class());
}
Answer the question
In order to leave comments, you need to log in
The extension that you brought allows you to manage the storage of the tree, and not the construction of an html list from it. And it works great.
But the output of the tree can be different, depending on the levels of nesting. The main disadvantage of the "adjacency list" principle is just the conclusion. If the nesting is unknown, it is necessary to write a recursive function, and if the level is 2, then the usual enumeration.
For example, we form an array with the desired hierarchy for the menu widget.
public static function getMenuList(){
$root = self::findOne(2); //2 это id root элемента
$leaves = $root->getChildren()->with('children')->all();
return \yii\helpers\ArrayHelper::toArray(
$leaves,
[
'common\models\Menu'=>[ //namespace модели
'label'=>'name',
'items'=> function($model){
return \yii\helpers\ArrayHelper::toArray(
$model->children,
[
'common\models\Menu'=>[
'label'=>'name',
]
]
);
},
]
]
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question