Gets the full tree from database

@return Select
481351
public function getFullTree()
    {
        $select = $this->getSql()->select();
        $select->from(['child' => $this->getTable()])
            ->columns([
                Select::SQL_STAR,
                'depth' => new Expression('(COUNT(parent.' . $this->getPrimaryKey() . ') - 1)')
            ])
            ->join(
                ['parent' => $this->getTable()],
                'child.' . self::COLUMN_LEFT . ' BETWEEN parent.' . self::COLUMN_LEFT . ' AND parent.' . self::COLUMN_RIGHT,
                [],
                Select::JOIN_INNER
            )
            ->group('child.' . $this->getPrimaryKey())
            ->order('child.' . self::COLUMN_LEFT);

        return $select;
    }
446785
public function getQuery()
    {
        $sql = $this->prepareSelectString();
        $sql .= $this->prepareJoinString();
        $sql .= $this->prepareWhereString();
        $sql .= $this->prepareGroupByString();
        $sql .= $this->prepareHavingString();
        $sql .= $this->prepareOrderByString();
        $sql .= $this->prepareLimitString();

        return $sql;
    }
1272594
public function findAll()
    {
        $ret = $this->conn()->selectCollection($this->table_name)->find($this->where, $this->field);
        if ($this->limit) {
            $ret->limit($this->limit)->skip($this->skip);
        }
        if ($this->orderby) {
            $ret->sort($this->orderby);
        }

        $this->sql = array(
                     'table'=> $this->table_name,
                     'query'=>$this->where,
                     'field'=> $this->field,
                     'limit'=> $this->limit,
                     'skip'=> $this->skip,
                   );
        $this->_clear_var();
        return $ret;
    }
768795
public function getDatabase()
    {
        if(!isset($this->_database))
        {
            $query = $this->getObject('lib:database.query.select')
                ->columns('DATABASE()');

            $this->_database = $this->select($query, KDatabase::FETCH_FIELD);
        }
        return $this->_database;
    }
386538
public function getSql()
    {
        if ($this->sql !== null && $this->state === self::STATE_CLEAN) {
            return $this->sql;
        }

        switch ($this->type) {
            case self::DELETE:
                $this->sql = $this->getSqlForDelete();
                break;

            case self::UPDATE:
                $this->sql = $this->getSqlForUpdate();
                break;

            case self::SELECT:
            default:
                $this->sql = $this->getSqlForSelect();
                break;
        }

        $this->state = self::STATE_CLEAN;

        return $this->sql;
    }
1374742
public static function selectTree(Collection $data, $selected = 0, $options = [], $htmlOptions = [])
    {
        $options = array_merge($options instanceof Std ? $options->toArray() : $options, ['tree']);
        if (empty($options['treeLevelColumn']))
            $options['treeLevelColumn'] = '__lvl';
        return self::select($data, $selected, $options, $htmlOptions);
    }
616782
protected function _getSelect()
    {
        $select = $this->_model->select();
        if ($this->_hasPosition) {
            $select->order('pos');
        } else if (!$select->hasPart('order') && $this->_defaultOrder) {
            $select->order(
                $this->_defaultOrder['field'],
                $this->_defaultOrder['direction']
            );
        }
        return $select;
    }
295160
public function getParent()
    {
        if ($this->isRoot()) throw new Exception\CMS('No parent for root Catalog');
    
        if (!$this->_parent) {
        
            $fields = self::getDbConnection()->fetchAssoc( 
                " SELECT A.*
                  FROM dir_data A, dir_structure B, dir_structure C 
                  WHERE C.data_id=? and B.data_id=A.id and B.lft<C.lft and B.rght>C.rght and B.level=C.level-1", 
                array( (int)$this->id )
            );        
        
            if ($fields) {
                $this->_parent = Catalog::fetch($fields);
            } else {
                $this->_parent = Catalog::getRoot();
            }
        }
        return $this->_parent;
    }
551604
public static function select()
    {
        $memoryTableAnnotation = ModelManager::getAnnotation(static::class, MemoryTable::class);
        if(null === $memoryTableAnnotation)
        {
            return null;
        }
        $instance = MemoryTableManager::getInstance($memoryTableAnnotation->name);
        return \iterator_to_array($instance);
    }
216325
protected static function makeSelect()
    {
        $self   = self::getInstance();
        $select = (!empty($self->optionSelect)) ? $self->optionSelect : "*";

        return "SELECT {$select} FROM {$self->table_name} ";
    }
1270056
public function getTree() {
    $children = array();
    $properties = $this->getChildProperties();
    $child = $this->head;
    $i = 0;
    while ($child) {
      $key = array_search($child, $properties, TRUE);
      if (!$key) {
        $key = $i;
      }
      if ($child instanceof ParentNode) {
        $children[$key] = $child->getTree();
      }
      else {
        /** @var TokenNode $child */
        $children[$key] = array($child->getTypeName() => $child->getText());
      }
      $child = $child->next;
      $i++;
    }
    $class_name = get_class($this);
    return array($class_name => $children);
  }
716899
public function getSelectClause()
    {
        $select = [];
        foreach ($this->all() as $column) {
            $select[] = $column->getRawSelect();
        }

        return implode(', ', $select);
    }
997155
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;
    }
1277097
private function getSQLForSelect()
    {
        $query = 'SELECT ' . implode(', ', $this->sqlParts['select']);

        $query .= ($this->sqlParts['from'] ? ' FROM ' . implode(', ', $this->getFromClauses()) : '')
            . ($this->sqlParts['where'] !== null ? ' WHERE ' . ((string) $this->sqlParts['where']) : '')
            . ($this->sqlParts['groupBy'] ? ' GROUP BY ' . implode(', ', $this->sqlParts['groupBy']) : '')
            . ($this->sqlParts['having'] !== null ? ' HAVING ' . ((string) $this->sqlParts['having']) : '')
            . ($this->sqlParts['orderBy'] ? ' ORDER BY ' . implode(', ', $this->sqlParts['orderBy']) : '');

        if ($this->isLimitQuery()) {
            return $this->connection->getDatabasePlatform()->modifyLimitQuery(
                $query,
                $this->maxResults,
                $this->firstResult
            );
        }

        return $query;
    }
883664
protected function getTable()
	{
		$table = parent::getTable();

		if ($table instanceof SelectQuery) return "(" . $table->get(false) . ")";

		return $table;
	}
108228
public function getCategoriesTree()
    {
        $allCategories = $this->getCategories();
        $topCategories = [];

        $currentCategory = null;

        // Building tree from categories collection
        foreach ($allCategories as $idCat => $category) {

            // If level, we put category in an array with only top level categories.
            $topLevel = 2; // 1 is root category
            if ($category->getLevel() == $topLevel && !isset($topCategories[$idCat])) {
                $topCategories[$idCat] = $category;
            } else {
                // Foreach categories with sub category we add a child to the parent
                if (!isset($allCategories[$category->getParentId()])) {
                    // Ignore if no parent
                    continue;
                }
                $parent           = $allCategories[$category->getParentId()];
                $children         = $parent->getData('_children') ?: [];
                $children[$idCat] = $category;
                $category->setData('_parent', $parent);
                $parent->setData('_children', $children);
            }

            if ((int) $idCat === $this->getCurrentCategoryId()) {
                $currentCategory = $category;
            }
        }

        // Set current path
        if (null !== $currentCategory) {
            $setCategoryAsCurrent = function (Mage_Catalog_Model_Category $category, $isParent = false) use (&$setCategoryAsCurrent) {
                if ($isParent) {
                    $category->setContainsCurrentCategory(true);
                } else {
                    $category->setIsCurrentCategory(true);
                }
                if ($parentCategory = $category->getData('_parent')) {
                    $setCategoryAsCurrent($parentCategory, true);
                }
            };
            $setCategoryAsCurrent($currentCategory);
        }

        return $topCategories;
    }
731974
public function getFullHierarchy()
    {
        $sep = $this->config()->hierarchy_seperator;

        return ($this->ParentID) ? $this->getBreadcrumbs($sep) : $this->Title;
    }