Возвращает имя и alias колонок для выборки полей
@param array $fields список выбираемых полей
@param string $fieldSourceAlias alias таблицы коллекции
@return array
196385
protected function getSelectedColumns(array $fields, $fieldSourceAlias)
    {
        $columns = [];

        foreach ($fields as $fieldName => $field) {
            if ($field instanceof ILocalizableField && $field->getIsLocalized()) {
                $currentLocaleId = $this->getCurrentLocale();
                $defaultLocaleId = $this->getDefaultLocale();

                if ($this->withLocalization) {
                    foreach ($field->getLocalizations() as $localeId => $localeInfo) {
                        $columnName = $fieldSourceAlias . self::FIELD_SEPARATOR . $localeInfo['columnName'];
                        $alias = $fieldSourceAlias . self::ALIAS_SEPARATOR . $fieldName
                            . ILocalizedProperty::LOCALE_SEPARATOR . $localeId;
                        $columns[] = [$columnName, $alias];
                    }
                } else {
                    $columnName = $fieldSourceAlias . self::FIELD_SEPARATOR
                        . $field->getLocaleColumnName($currentLocaleId);
                    $alias = $fieldSourceAlias . self::ALIAS_SEPARATOR . $fieldName
                        . ILocalizedProperty::LOCALE_SEPARATOR . $currentLocaleId;
                    $columns[] = [$columnName, $alias];

                    if ($currentLocaleId !== $defaultLocaleId) {
                        $columnName = $fieldSourceAlias . self::FIELD_SEPARATOR
                            . $field->getLocaleColumnName($defaultLocaleId);
                        $alias = $fieldSourceAlias . self::ALIAS_SEPARATOR . $fieldName
                            . ILocalizedProperty::LOCALE_SEPARATOR . $defaultLocaleId;
                        $columns[] = [$columnName, $alias];
                    }
                }

            } else {
                $columnName = $fieldSourceAlias . self::FIELD_SEPARATOR . $field->getColumnName();
                $alias = $fieldSourceAlias . self::ALIAS_SEPARATOR . $fieldName;
                $columns[] = [$columnName, $alias];
            }
        }

        return $columns;
    }
1147917
public function select(string $fields): QueryBuilderInterface
	{
		// Split fields by comma
		$fieldsArray = explode(',', $fields);
		$fieldsArray = array_map('mb_trim', $fieldsArray);

		// Split on 'As'
		foreach ($fieldsArray as $key => $field)
		{
			if (stripos($field, 'as') !== FALSE)
			{
				$fieldsArray[$key] = preg_split('` as `i', $field);
				$fieldsArray[$key] = array_map('mb_trim', $fieldsArray[$key]);
			}
		}

		// Quote the identifiers
		$safeArray = $this->driver->quoteIdent($fieldsArray);

		unset($fieldsArray);

		// Join the strings back together
		for($i = 0, $c = count($safeArray); $i < $c; $i++)
		{
			if (\is_array($safeArray[$i]))
			{
				$safeArray[$i] = implode(' AS ', $safeArray[$i]);
			}
		}

		$this->state->appendSelectString(implode(', ', $safeArray));

		return $this;
	}
1371320
public function mapFormFields($fields)
    {
        $_fields = [];

        foreach ($fields as $key => $options) {
            $_options = $this->config('formFieldOptions');

            if (is_array($options)) {
                $_fields[$key] = array_merge($_options, $options);
            } else {
                $_fields[$options] = $_options;
            }
        }

        return $_fields;
    }
1022471
protected function map($fields)
    {
        $list = [];
        if (count($this->columns) !== count($fields)) {
            throw new \RuntimeException('Column mismatch on line ' . $this->line);
        }
        foreach ($this->columns as $column) {
            $list[$column] = array_shift($fields);
        }
        return $list;
    }
1159950
public function select($fields)
    {
        foreach ($fields as $key => $value) {
            if (is_int($key)) {
                $this->associationQueryFields($value);
            } elseif (is_array($value)) {
                $alias = $this->tableAlias();
                $this->queryBuilder->join($this->getFullFieldName($key, self::DEFAULT_TABLE_ALIAS), $alias);
                foreach ($value as $valueField) {
                    $this->queryFields[] = $this->getFullFieldName($valueField, $alias);
                }
            } else {
                $this->queryFields[] = $value;
            }
        }
        $this->queryBuilder->select(implode(',', $this->queryFields));

        return $this;
    }
117500
protected function objectifyFields($fields) {
        $new = array();
        foreach($fields as $name => $field) {
            if (is_object($field)) {
                $new[] = $field;
            } else {
                $new[] = new EntryField($name, $field);
            }
        }
        return $new;
    }
822370
public function iterator(array $fields = null)
    {
        if ($this->qiterator) {
            return $this->qiterator;
        }
        $aliases = [];
        $getAlias = function ($name) use (&$aliases) {
            // to bypass use: return $name;
            return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases);
        };
        $table = $this->definition->getName();
        if ($fields !== null) {
            $this->columns($fields);
        }
        $relations = $this->withr;
        foreach ($relations as $k => $v) {
            $getAlias($k);
        }

        $f = $this->fields;
        $w = $this->where;
        $h = $this->having;
        $o = $this->order;
        $g = $this->group;
        $j = array_map(function ($v) { return clone $v; }, $this->joins);

        $porder = [];
        foreach ($this->definition->getPrimaryKey() as $field) {
            $porder[] = $this->getColumn($field)['name'];
        }

        foreach ($this->definition->getRelations() as $k => $relation) {
            foreach ($f as $kk => $field) {
                if (strpos($field, $k . '.') === 0) {
                    $relations[$k] = [ $relation, $table ];
                    $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field);
                }
            }
            foreach ($w as $kk => $v) {
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
                    $relations[$k] = [ $relation, $table ];
                    $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
                }
            }
            foreach ($h as $kk => $v) {
                if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) {
                    $relations[$k] = [ $relation, $table ];
                    $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]);
                }
            }
            if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) {
                $relations[$k] = [ $relation, $table ];
                $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]);
            }
            if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) {
                $relations[$k] = [ $relation, $table ];
                $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]);
            }
            foreach ($j as $kk => $v) {
                foreach ($v->keymap as $kkk => $vv) {
                    if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) {
                        $relations[$k] = [ $relation, $table ];
                        $j[$k]->keymap[$kkk] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv);
                    }
                }
            }
        }
        $select = [];
        foreach ($f as $k => $field) {
            $select[] = $field . (!is_numeric($k) ? ' ' . $k : '');
        }
        foreach ($this->withr as $name => $relation) {
            foreach ($relation[0]->table->getColumns() as $column) {
                $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column);
            }
        }
        $sql = 'SELECT '.implode(', ', $select).' FROM '.$table.' ';
        $par = [];
        $many = false;
        foreach ($j as $k => $v) {
            if ($v->many) {
                $many = true;
            }
            $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON ';
            $tmp = [];
            foreach ($v->keymap as $kk => $vv) {
                $tmp[] = $kk.' = '.$vv;
            }
            $sql .= implode(' AND ', $tmp) . ' ';
        }
        foreach ($relations as $relation => $v) {
            $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1];
            $v = $v[0];
            if ($v->many || $v->pivot) {
                $many = true;
            }
            if ($v->pivot) {
                $alias = $getAlias($relation.'_pivot');
                $sql .= 'LEFT JOIN '.$v->pivot->getName().' '.$alias.' ON ';
                $tmp = [];
                foreach ($v->keymap as $kk => $vv) {
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
                }
                $sql .= implode(' AND ', $tmp) . ' ';
                $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($relation).' ON ';
                $tmp = [];
                foreach ($v->pivot_keymap as $kk => $vv) {
                    $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' ';
                }
                $sql .= implode(' AND ', $tmp) . ' ';
            } else {
                $alias = $getAlias($relation);

                $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON ';
                $tmp = [];
                foreach ($v->keymap as $kk => $vv) {
                    $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' ';
                }
                if ($v->sql) {
                    $tmp[] = $v->sql . ' ';
                    $par = array_merge($par, $v->par ?? []);
                }
                $sql .= implode(' AND ', $tmp) . ' ';
            }
        }

        if ($many && count($porder) && $this->li_of[2] === 1) {
            $ids = $this->ids();
            if (count($ids)) {
                if (count($porder) > 1) {
                    $pkw = [];
                    foreach ($porder as $name) {
                        $pkw[] = $name . ' = ?';
                    }
                    $pkw = '(' . implode(' AND ', $pkw) . ')';
                    $pkp = [];
                    foreach ($ids as $id) {
                        foreach ($id as $p) {
                            $pkp[] = $p;
                        }
                    }
                    $w[] = [
                        implode(' OR ', array_fill(0, count($ids), $pkw)),
                        $pkp
                    ];
                } else {
                    $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ];
                }
            } else {
                $w[] = [ '1=0', [] ];
            }
        }
        if (count($w)) {
            $sql .= 'WHERE ';
            $tmp = [];
            foreach ($w as $v) {
                $tmp[] = '(' . $v[0] . ')';
                $par = array_merge($par, $v[1]);
            }
            $sql .= implode(' AND ', $tmp).' ';
        }
        if (count($g)) {
            $sql .= 'GROUP BY ' . $g[0] . ' ';
            $par = array_merge($par, $g[1]);
        }
        if (count($h)) {
            $sql .= 'HAVING ';
            $tmp = [];
            foreach ($h as $v) {
                $tmp[] = '(' . $v[0] . ')';
                $par = array_merge($par, $v[1]);
            }
            $sql .= implode(' AND ', $tmp).' ';
        }
        if (count($o)) {
            $sql .= 'ORDER BY ' . $o[0] . ' ';
            $par = array_merge($par, $o[1]);
        }
        if (count($porder)) {
            $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC';
            $porder = array_map(function ($v) use ($pdir) { return $v . ' ' . $pdir; }, $porder);
            $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' ';
        }
        if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) {
            if ($this->db->driverName() === 'oracle') {
                if ((int)$this->db->driverOption('version', 0) >= 12) {
                    $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY';
                } else {
                    $f = array_map(function ($v) {
                        $v = explode(' ', trim($v), 2);
                        if (count($v) === 2) { return $v[1]; }
                        $v = explode('.', $v[0], 2);
                        return count($v) === 2 ? $v[1] : $v[0];
                    }, $select);
                    $sql = "SELECT " . implode(', ', $f) . " 
                            FROM (
                                SELECT tbl__.*, rownum rnum__ FROM (
                                    " . $sql . "
                                ) tbl__ 
                                WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . "
                            ) WHERE rnum__ > " . $this->li_of[1];
                }
            } else {
                $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1];
            }
        }
        return $this->qiterator = new TableQueryIterator(
            $this->db->get($sql, $par, null, false, false), 
            $this->pkey,
            $this->withr,
            $aliases
        );
    }
233225
public function fields($fields = [])
    {
        $this->builder->select([]);
        $this->casts = [];

        if (empty($fields)) {
            foreach ($this->model->fields() as $field) {
                $this->assignField($field);
            }

            return $this;
        }

        foreach ($fields as $field) {
            $this->assignField($this->model->field($field));
        }

        return $this;
    }
408550
protected function filterFields(array $fields)
    {
        if ($requested = $this->getOwnFields()) {
            $fields = array_intersect_key($fields, array_flip($requested));
        }

        return $fields;
    }
423163
protected function getMergedFields(array $fields)
    {
        if (0 === \count($fields)) {
            // If we didn't specify the fields (filtered), we get all variation fields possible values
            $fields = $this->getVariationFields();
        } else {
            foreach ($fields as $field) {
                if (!\in_array($field, $this->getVariationFields(), true)) {
                    throw new \RuntimeException("The field '".$field."' is not among the variation fields");
                }
            }
        }

        return $fields;
    }
18191
private function getMainFields(array $fields): array
    {
        $main = [];
        foreach ($fields as $field) {
            if ($field instanceof ChildrenAssociationField) {
                continue;
            }

            if ($field->is(Deferred::class)) {
                continue;
            }

            if (!$field->is(PrimaryKey::class)) {
                $main[] = $field;
                continue;
            }

            if ($field instanceof FkField) {
                $main[] = $field;
            }
        }

        return $main;
    }
492762
protected function getFormPlaceholders(array $fields)
    {
        $attributes = [];

        foreach ($fields as $field) {
            /** @var FieldTypeInterface $field */
            $attributes[$field->getName()] = $field->getOption('placeholder');
        }

        return $attributes;
    }
36828
public static function find($fields = '*')
    {
        $entity = new static();
        if ($fields == '*') {
            $fields = $entity->getTableName() .'.*';
        }
        Entity\Manager::getInstance()->get($entity)->refreshRelations();
        $select = new \Slick\Orm\Sql\Select($entity, $fields);
        return $select;
    }
907535
public function desc($fields) {
        if (is_array($fields)) {
            $this->_order = ' ORDER BY ' . implode($fields, ', ') . ' DESC';
        } else {
            $this->_order = ' ORDER BY ' . $fields . ' DESC';
        }
         
        return $this;
    }
408518
public function fields($fields) {

        if ( is_array($fields) ) {
            $this->fields = '`'.implode("`, `", $fields).'`';
        } else if ( is_string($fields) ) {
            $this->fields = $fields;
        } else {
            $this->fields = '*';
        }
        return $this;
    }
1207105
protected static function cleanFields($fields)
    {
        $new = [];
        $fieldList = explode(',', $fields);
        foreach ($fieldList as $f) {
            if (static::ID_FIELD !== trim(strtolower($f))) {
                $new[] = $f;
            }
        }

        return implode(',', $new);
    }
544788
private static function _autoUpdateFromFields(&$fields)
    {
        $addKeywords = [null, null];
        $removeKeywords = [null, null];

        if (isset($fields->add) && isset($fields->add->first_word)) {
            $addKeywords[0] = $fields->add->first_word;
        }

        if (isset($fields->add) && isset($fields->add->second_word)) {
            $addKeywords[1] = $fields->add->second_word;
        }

        if (isset($fields->remove) && isset($fields->remove->first_word)) {
            $removeKeywords[0] = $fields->remove->first_word;
        }

        if (isset($fields->remove) && isset($fields->remove->second_word)) {
            $removeKeywords[1] = $fields->remove->second_word;
        }

        return new Api\GroupAutoUpdate(
            $fields->to, $addKeywords, $removeKeywords
        );
    }