src/Titon/Db/Behavior/HierarchyBehavior.php
public function getTree($id = null) {
        $parent = $this->getConfig('parentField');
        $left = $this->getConfig('leftField');
        $right = $this->getConfig('rightField');

        $query = $this->getRepository()->select()->orderBy($left, 'asc');

        if ($id) {
            if ($parentNode = $this->getNode($id)) {
                $query->where($left, 'between', [$parentNode[$left], $parentNode[$right]]);
            } else {
                return [];
            }
        }

        $nodes = $query->all();

        if ($nodes->isEmpty()) {
            return [];
        }

        $map = [];
        $stack = [];
        $pk = $this->getRepository()->getPrimaryKey();

        foreach ($nodes as $node) {
            if ($node[$parent] && $node[$pk] != $id) {
                $map[$node[$parent]][] = $node;
            } else {
                $stack[] = $node;
            }
        }

        $results = $this->mapTree($stack, $map);

        if ($id) {
            return $results[0];
        }

        return $results;
    }
public function getTree($id = null) {
        $parent = $this->getConfig('parentField');
        $left = $this->getConfig('leftField');
        $right = $this->getConfig('rightField');

        $query = $this->getRepository()->select()->orderBy($left, 'asc');

        if ($id) {
            if ($parentNode = $this->getNode($id)) {
                $query->where($left, 'between', [$parentNode[$left], $parentNode[$right]]);
            } else {
                return [];
            }
        }

        $nodes = $query->all();

        if ($nodes->isEmpty()) {
            return [];
        }

        $map = [];
        $stack = [];
        $pk = $this->getRepository()->getPrimaryKey();

        foreach ($nodes as $node) {
            if ($node[$parent] && $node[$pk] != $id) {
                $map[$node[$parent]][] = $node;
            } else {
                $stack[] = $node;
            }
        }

        $results = $this->mapTree($stack, $map);

        if ($id) {
            return $results[0];
        }

        return $results;
    }
public function getTree ( $ id = null ) { $ parent = $ this -> getConfig ( 'parentField' ) ; $ left = $ this -> getConfig ( 'leftField' ) ; $ right = $ this -> getConfig ( 'rightField' ) ; $ query = $ this -> getRepository ( ) -> select ( ) -> orderBy ( $ left , 'asc' ) ; if ( $ id ) { if ( $ parentNode = $ this -> getNode ( $ id ) ) { $ query -> where ( $ left , 'between' , [ $ parentNode [ $ left ] , $ parentNode [ $ right ] ] ) ; } else { return [ ] ; } } $ nodes = $ query -> all ( ) ; if ( $ nodes -> isEmpty ( ) ) { return [ ] ; } $ map = [ ] ; $ stack = [ ] ; $ pk = $ this -> getRepository ( ) -> getPrimaryKey ( ) ; foreach ( $ nodes as $ node ) { if ( $ node [ $ parent ] && $ node [ $ pk ] != $ id ) { $ map [ $ node [ $ parent ] ] [ ] = $ node ; } else { $ stack [ ] = $ node ; } } $ results = $ this -> mapTree ( $ stack , $ map ) ; if ( $ id ) { return $ results [ 0 ] ; } return $ results ; }
Return a tree of nested nodes. If no ID is provided, the top level root will be used.

@param int $id
@return array
Return a tree of nested nodes . If no ID is provided the top level root will be used .