G
G
gachkydxvbgd2017-10-07 02:33:52
Yii
gachkydxvbgd, 2017-10-07 02:33:52

how to get tree in yii2?

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

- menu
      - menu
      - menu
 - menu
      - menu
      - menu

Tried via https://github.com/paulzi/yii2-adjacency-list
public static function find()
    {
        return new SampleQuery(get_called_class());
    }

This function throws an error
. How can this be implemented without third-party libs?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-10-07
@gachkydxvbgd

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',
                            ]
                        ]
                    );
                  },
              ]
          ]
      );

I planned to record a video on this extension today, it will be here in the evening:
https://www.youtube.com/channel/UC3jTSXXgSvQI2WJ5f...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question