Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Как получить все деревья #116

Open
tidzh opened this issue Mar 8, 2018 · 2 comments
Open

Как получить все деревья #116

tidzh opened this issue Mar 8, 2018 · 2 comments

Comments

@tidzh
Copy link

tidzh commented Mar 8, 2018

Добрый вечер. не смог найти решение. Появилась необходимость в виджете Select2 вывести все категории с вложенностью. Т.е получается нужно вывести всех родителей с детьми

@dbykadorov
Copy link

Если еще актуально - для каждого рута надо получить его детей и т.д. рекурсивно. Так все дерево построите.

@yamalweb
Copy link

yamalweb commented Apr 6, 2018

public static function getTree($categories, $left = 0, $right = null, $lvl = 1){

        $tree = [];

        foreach ($categories as $index => $category) {
            if ($category->lft >= $left + 1 && (is_null($right) || $category->rgt <= $right) && $category->lvl == $lvl) {
                $tree[$index] = [
                    'id' => $category->id,
                    'label' => $category->name,
                    'url' => $category->url,
                    'items' => self::getTree($categories, $category->lft, $category->rgt, $category->lvl + 1),
                ];
            }
        }

        return $tree;

    }

    public static function getFullTreeStructure(){

        //$roots = Menu::find()->where(['root' => 1])->addOrderBy('root, lft')->all();
        $roots = Menu::find()->roots()->addOrderBy('root, lft')->all();
        $tree = [];
        foreach ($roots as $root){
            $tree [] = [
                'id' => $root->id,
                'label' => $root->name,
                'url' => $root->url,
                'items' => self::getTree($root->children()->all()),
            ];
        }
        return $tree;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants