Get Structure of the current table

@param bool $withGuarded
@param bool $asKey
@return array
@throws \Exception
491813
public static function getStructure($withoutGuarded = false, $asKey = false)
    {
        $t = new self;

	    # if structure is defined => return structure

	    if(isset(static::$_structure)){
		    return static::$_structure;
	    }

        $table = $t->getTable();

        $columns = array();

        switch (DB::connection()->getConfig('driver')) {
            case 'pgsql':
                $query = "SELECT column_name FROM information_schema.columns WHERE table_name = '".$table."'";
                $column_name = 'column_name';
                $reverse = true;
                break;

            case 'mysql':
                $query = 'SHOW COLUMNS FROM '.$table;
                $column_name = 'Field';
                $reverse = false;
                break;

            case 'sqlsrv':
                $parts = explode('.', $table);
                $num = (count($parts) - 1);
                $table = $parts[$num];
                $query = "SELECT column_name FROM ".DB::connection()->getConfig('database').".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'".$table."'";
                $column_name = 'column_name';
                $reverse = false;
                break;

            default:
                # Try to guess structure with first Element
                $table = self::where('id', '!=', 'x')->first();

                if($table){
                    $columns = array_keys($table->getAttributes());
                } else {
                    $error = 'Database driver not supported: you must define a static _structure variable '.DB::connection()->getConfig('driver');
                    throw new \Exception($error);
                }
                break;
        }

        if (!$columns) {
            $columns = array();

            foreach(DB::select($query) as $column)
            {
                $columns[] = $column->$column_name;
            }

            if($reverse)
            {
                $columns = array_reverse($columns);
            }
        }

        if($withoutGuarded){

            $kColumns = array_flip($columns);

            foreach($t->guarded as $key){

                if(isset($kColumns[$key])){

                    unset($columns[$kColumns[$key]]);
                }
            }

            # remove timestamp

            $updated_at = array_search('updated_at', $columns);

            if($updated_at !== false){
                unset($columns['updated_at']);
            }

            $created_at = array_search('created_at', $columns);

            if($created_at !== false){
                unset($columns['created_at']);
            }

        }

        # Switch keys if we want column name as key
        if($asKey)
        {
            $columns = array_flip($columns);
        }

        return $columns;
    }
544010
public function get($data, $with = [], $cache = false, $failException = false)
    {
        if (is_null($data)) {
            return;
        }

        if (true === $with || is_int($with)) {
            $cache = $with;
            $with  = [];
        }

        return $this->parseQuery($data, $with, $cache)
            ->failException($failException)
            ->find($data);
    }
297430
public function all(array $with = [], $all = false)
    {
        $query = $this->with($with);

        if (!$all) {
            $query->published();
        }

        // Query ORDER BY
        $query->order();

        // Get
        return $query->get();
    }
117042
public function getParentIdColumnName(bool $withTableName = false): string
    {
        return ($withTableName) ? $this->addTableName($this->parentIdColumnName) : $this->parentIdColumnName;
    }
1102058
public function loadFile($with = array())
    {
        return $this->getManager()->loadModel($this->owner->{$this->idAttribute}, $with);
    }
662945
public function getMessages($withWarning=false) {
		if ($withWarning) $this->messageList->allErrorOrWarningArray();
		return $this->messageList->allErrorArray();
	}
1278632
public function get($sKey = null, $sGrouping = 'app', $bForceRefresh = false)
    {
        if (!isset($this->aSettings[$sGrouping]) || $bForceRefresh) {

            $oDb = Factory::service('Database');
            $oDb->where('grouping', $sGrouping);
            $aSettings = $oDb->get($this->sTable)->result();

            $this->aSettings[$sGrouping] = [];

            foreach ($aSettings as $oSetting) {

                $sValue = $oSetting->value;

                if (!empty($oSetting->is_encrypted)) {
                    $oEncrypt = Factory::service('Encrypt');
                    $sValue   = $oEncrypt->decode($sValue);
                }

                $this->aSettings[$sGrouping][$oSetting->key] = json_decode($sValue);
            }
        }

        // --------------------------------------------------------------------------

        if (empty($sKey)) {

            return $this->aSettings[$sGrouping];

        } else {

            return isset($this->aSettings[$sGrouping][$sKey]) ? $this->aSettings[$sGrouping][$sKey] : null;
        }
    }
126359
public function getItem($cacheId, $withVersion = true, &$success = null)
    {
        if ($withVersion) {
            $cacheId .= '.' . $this->getVersion();
        }

        if (!$this->cache->hasItem($cacheId)) {
            $success = false;

            return null;
        }

        $success = null;
        $data    = $this->cache->getItem($cacheId, $success);

        if (!$success) {
            $success = false;

            return null;
        }

        if (!property_exists($data, 'content')) {
            $success = false;

            return null;
        }

        $success = true;

        return $data->content;
    }
1273273
public function fresh($with = [])
    {
        if (! $this->exists) {
            return;
        }

        return static::newQueryWithoutScopes()
                        ->with(is_string($with) ? func_get_args() : $with)
                        ->where($this->getKeyName(), $this->getKey())
                        ->first();
    }
730571
public function find($id, array $with = [])
    {
        $query = $this->make($with);

        return $query->find($id);
    }
34332
protected function normalizeSchema(bool $withForeignKeys = true)
    {
        // To make sure that no pre-sync modifications will be reflected on current table
        $target = clone $this;

        // declare all FKs dropped on tables scheduled for removal
        if ($this->status === self::STATUS_DECLARED_DROPPED) {
            foreach ($target->getForeignKeys() as $fk) {
                $target->current->forgerForeignKey($fk);
            }
        }

        /*
         * In cases where columns are removed we have to automatically remove related indexes and
         * foreign keys.
         */
        foreach ($this->getComparator()->droppedColumns() as $column) {
            foreach ($target->getIndexes() as $index) {
                if (in_array($column->getName(), $index->getColumns())) {
                    $target->current->forgetIndex($index);
                }
            }

            foreach ($target->getForeignKeys() as $foreign) {
                if ($column->getName() == $foreign->getColumn()) {
                    $target->current->forgerForeignKey($foreign);
                }
            }
        }

        //We also have to adjusts indexes and foreign keys
        foreach ($this->getComparator()->alteredColumns() as $pair) {
            /**
             * @var AbstractColumn $initial
             * @var AbstractColumn $name
             */
            list($name, $initial) = $pair;

            foreach ($target->getIndexes() as $index) {
                if (in_array($initial->getName(), $index->getColumns())) {
                    $columns = $index->getColumns();

                    //Replacing column name
                    foreach ($columns as &$column) {
                        if ($column == $initial->getName()) {
                            $column = $name->getName();
                        }

                        unset($column);
                    }

                    $targetIndex = $target->initial->findIndex($index->getColumns());
                    if (!empty($targetIndex)) {
                        //Target index got renamed or removed.
                        $targetIndex->columns($columns);
                    }

                    $index->columns($columns);
                }
            }

            foreach ($target->getForeignKeys() as $foreign) {
                if ($initial->getName() == $foreign->getColumn()) {
                    $foreign->column($name->getName());
                }
            }
        }

        if (!$withForeignKeys) {
            foreach ($this->getComparator()->addedForeignKeys() as $foreign) {
                //Excluding from creation
                $target->current->forgerForeignKey($foreign);
            }
        }

        return $target;
    }
891748
public function getLeftColumnName(bool $withTableName = false): string
    {
        return ($withTableName) ? $this->addTableName($this->leftColumnName) : $this->leftColumnName;
    }
192678
public function getIdColumnName(bool $withTableName = false): string
    {
        return ($withTableName) ? $this->addTableName($this->idColumnName) : $this->idColumnName;
    }
729452
public function findWhere($where, $columns = ['*'], $with = [])
    {
        $where    = $this->castRequest($where);
        $cacheKey = $this->generateKey([$where, $columns, $with]);

        return $this->cacheResults(get_called_class(), __FUNCTION__, $cacheKey, function () use ($where, $columns, $with) {
            $model = $this->model instanceof Model ? $this->model->query() : $this->model;

            foreach ($where as $attribute => $value) {
                if (is_array($value)) {
                    list($attribute, $condition, $value) = $value;

                    $model->where($attribute, $condition, $value);
                } else {
                    $model->where($attribute, '=', $value);
                }
            }

            return $model->with($with)
                ->get($columns);
        });
    }
204558
public function getHlblock($hlblockName) {
        if (is_array($hlblockName)) {
            $filter = $hlblockName;
        } elseif (is_numeric($hlblockName)) {
            $filter = array('ID' => $hlblockName);
        } else {
            $filter = array('NAME' => $hlblockName);
        }

        $result = HL\HighloadBlockTable::getList(
            array(
                'select' => array('*'),
                'filter' => $filter,
            )
        );

        $hlblock = $result->fetch();
        if ($hlblock) {
            $hlblock['LANG'] = $this->getHblockLangs($hlblock['ID']);
        }

        return $hlblock;
    }
52826
public function allWith($with, $limit = null)
    {
        if ($limit) {
            return $this->model->with($with)->limit($limit)->get();
        }

        return $this->model->with($with)->get();

    }
333544
protected function buildTables($with_alias=true) {
		$tables = [];
		$params = [];
		if(!$this->tables)
			throw new \Exception('Must set tables with method from($tables) before running the query.');
		foreach($this->tables as $alias=>$table) {
			if($table instanceof static) {
				$tables[] = '('.$table->buildSQL().') '.$this->quote.$alias.$this->quote;
				$params = array_merge($params, $table->getParameters());
			}
			elseif($table instanceof Raw) {
				$tables[] = '('.$table->getSQL().') '.$this->quote.$alias.$this->quote;
				$params = array_merge($params, $table->getParameters());
			}
			elseif($alias !== $table && $with_alias)
				$tables[] = $this->quote.$table.$this->quote.' '.$this->quote.$alias.$this->quote;
			else
				$tables[] = $this->quote.$table.$this->quote;
		}
		$sql = implode(', ', $tables);
		return [$sql, $params];
	}