src/MaterializedPathBehavior.php
public function populateTree($depth = null)
    {
        /** @var ActiveRecord|MaterializedPathBehavior $nodes */
        $nodes = $this
            ->getDescendants($depth)
            ->indexBy($this->keyAttribute)
            ->all();
        $relates = [];
        foreach ($nodes as $key => $node) {
            $parentKey = $node->getParentKey();
            if (!isset($relates[$parentKey])) {
                $relates[$parentKey] = [];
            }
            $relates[$parentKey][] = $node;
        }
        $nodes[$this->owner->{$this->keyAttribute}] = $this->owner;
        foreach ($relates as $key => $children) {
            $nodes[$key]->populateRelation('children', $children);
        }
        return $this->owner;
    }
public function populateTree($depth = null)
    {
        /** @var ActiveRecord|MaterializedPathBehavior $nodes */
        $nodes = $this
            ->getDescendants($depth)
            ->indexBy($this->keyAttribute)
            ->all();
        $relates = [];
        foreach ($nodes as $key => $node) {
            $parentKey = $node->getParentKey();
            if (!isset($relates[$parentKey])) {
                $relates[$parentKey] = [];
            }
            $relates[$parentKey][] = $node;
        }
        $nodes[$this->owner->{$this->keyAttribute}] = $this->owner;
        foreach ($relates as $key => $children) {
            $nodes[$key]->populateRelation('children', $children);
        }
        return $this->owner;
    }
public function populateTree ( $ depth = null ) { /** @var ActiveRecord|MaterializedPathBehavior $nodes */ $ nodes = $ this -> getDescendants ( $ depth ) -> indexBy ( $ this -> keyAttribute ) -> all ( ) ; $ relates = [ ] ; foreach ( $ nodes as $ key => $ node ) { $ parentKey = $ node -> getParentKey ( ) ; if ( ! isset ( $ relates [ $ parentKey ] ) ) { $ relates [ $ parentKey ] = [ ] ; } $ relates [ $ parentKey ] [ ] = $ node ; } $ nodes [ $ this -> owner -> { $ this -> keyAttribute } ] = $ this -> owner ; foreach ( $ relates as $ key => $ children ) { $ nodes [ $ key ] -> populateRelation ( 'children' , $ children ) ; } return $ this -> owner ; }
Returns descendants nodes as tree with self node in the root.

@param int $depth = null
@return MaterializedPathBehavior|ActiveRecord
Returns descendants nodes as tree with self node in the root .