Adds join statement to basic query from relations declared in config files

@param \yii\db\Query $query
@param string $table
@return array $columns to select statement
979329
public function getRelations(&$query, $table)
    {
        $relations = [];
        $columns = [];
        /** @var Module $module */
        $module = $this->module;
        if (isset($module->tables[$table]['relations'])) {
            $relations = $module->tables[$table]['relations'];
        }
        foreach ($relations as $relation => $relationParams) {
            $query->join(
                $relationParams['type'],
                $relationParams['table'] . ' ' . $relationParams['alias'],
                $relationParams['on']
            );
            $columns[] = $relationParams['alias'] . '.' . $relationParams['representive_columns'] . ' ' . $relation;
        }

        return $columns;
    }
1048588
public function filterQuery(QueryBuilder &$query, &$selects = null)
    {
        //run the parent method
        parent::filterQuery($query, $selects);

        //get the values
        $value   = $this->getOption('value');
        $table   = $this->getOption('table');
        $column  = $this->getOption('column');
        $column2 = $this->getOption('column2');

        //if there is no value, return
        if (!$value) {
            return;
        }

        $model = $this->config->getDataModel();

        //if the table hasn't been joined yet, join it
        if (!$this->validator->isJoined($query, $table)) {
            $query->join($table, $model->getTable().'.'.$model->getKeyName(), '=', $column);
        }

        //add where clause
        $query->whereIn($column2, $value);

        //add having clauses
        $query->havingRaw('COUNT(DISTINCT '.$query->getConnection()->getTablePrefix().$column2.') = '.count($value));

        //add select field
        if ($selects && !in_array($column2, $selects)) {
            $selects[] = $column2;
        }
    }
646594
public function delete(Query $query)
    {
        $options = $query->getOptions();

        return str_replace(
            ['%TABLE%', '%USING%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
            [
                $this->parseTable($query, $options['table']),
                !empty($options['using']) ? ' USING ' . $this->parseTable($query, $options['using']) . ' ' : '',
                $this->parseJoin($query, $options['join']),
                $this->parseWhere($query, $options['where']),
                $this->parseOrder($query, $options['order']),
                $this->parseLimit($query, $options['limit']),
                $this->parseLock($query, $options['lock']),
                $this->parseComment($query, $options['comment']),
            ],
            $this->deleteSql);
    }
586845
public function insertUsing(array $columns, $query)
    {
        [$sql, $bindings] = $this->createSub($query);

        return $this->connection->insert(
            $this->grammar->compileInsertUsing($this, $columns, $sql),
            $this->cleanBindings($bindings)
        );
    }
180347
public function rawJoin($query, $constraint, $tableAlias)
    {
        $this->hasJoin = true;

        // Add table alias if present
        if (!is_null($tableAlias)) {
            $tableAlias = $this->quoteIdentifier($tableAlias);
            $query .= " {$tableAlias}";
        }

        // Build the constraint
        if (is_array($constraint)) {
            list($firstColumn, $operator, $secondColumn) = $constraint;
            $firstColumn = $this->quoteIdentifier($firstColumn);
            $secondColumn = $this->quoteIdentifier($secondColumn);
            $constraint = "{$firstColumn} {$operator} {$secondColumn}";
        }

        $this->joinSources[] = "{$query} ON {$constraint}";

        return $this;
    }
103153
private function buildSelectJoinPart(ISelectBuilder $query)
    {
        $joins = $query->getJoins();
        if (!count($joins)) {
            return '';
        }

        $result = '';

        foreach ($joins as $join) {
            list($name, $alias) = $join->getTable();
            $result .= "\n\t" . $join->getType() . ' JOIN ';
            $result .= $this->quoteIdentifier($name) . ($alias ? ' AS ' . $this->quoteIdentifier($alias) : '');
            $joinConditions = [];
            foreach ($join->getConditions() as $condition) {
                list($leftColumn, $operator, $rightColumn) = $condition;
                $joinConditions[] = $this->quoteIdentifier($leftColumn)
                    . ' ' . $operator . ' ' . $this->quoteIdentifier($rightColumn);
            }

            if (count($joinConditions) === 1) {
                $result .= ' ON ' . $joinConditions[0];
            } elseif (count($joinConditions) > 1) {
                $result .= ' ON (' . implode(' AND ', $joinConditions) . ')';
            }
        }

        return $result;
    }
1174343
public function _addIdColumnConditions(&$query)
        {
            $query[] = "WHERE";
            $keys = Arrays::is($this->_getIdColumnName()) ? $this->_getIdColumnName() : array( $this->_getIdColumnName() );
            $first = true;

            foreach($keys as $key) {
                if ($first) {
                    $first = false;
                } else {
                    $query[] = "AND";
                }

                $query[] = $this->_quoteIdentifier($key);
                $query[] = "= ?";
            }
        }
1093709
private function performJoin($query, string $table, string $foreign, string $other)
    {
        // Check that table not joined yet
        $joins = [];
        foreach ((array)$query->getQuery()->joins as $key => $join) {
            $joins[] = $join->table;
        }

        if (!in_array($table, $joins)) {
            $query->leftJoin($table, $foreign, '=', $other);
        }

        return $query;
    }
230925
public function mergeToQuery(Select $query)
    {
        $query->join(
            $this->getKeyCondition(),
            $this->getQuery()->getWhere()->build(),
            null,
            $this->getQuery()->getBinds('where')
        );

        foreach ($this->select as $select) {
            $query->prependSelect($select);
        }

        foreach ($this->getQuery()->getSelect() as $key => $select) {
            $query->prependSelect([$key => $select]);
        }

        foreach ($this->getQuery()->getJoin() as $join) {
            $query->join($join, null, null, $this->getQuery()->getBinds('join', true));
        }

        if ($groupBy = $this->getQuery()->getGroupBy()) {
            $query->groupBy($groupBy);
        }

        if (($having = $this->getQuery()->getHaving()) && $having->hasChildren()) {
            $query->having($having);
        }

        if ($orderBy = $this->getQuery()->getOrderBy()) {
            $query->orderBy($orderBy);
        }

        return $this;
    }
222876
public function applyToModelQuery($query, $table = null)
    {
        if (is_null($this->scope) || $this->onlyScopeRelations) {
            return $query;
        }

        if (is_null($table)) {
            $table = $query->getModel()->getTable();
        }

        return $this->applyToQuery($query, $table);
    }
8002
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
    {
        $query = parent::getRelationQuery($query, $parent, $columns);

        return $query->where($this->morphType, $this->morphClass);
    }
843912
protected function mappedRelationQuery($query, $method, ArgumentBag $args, $mapping)
    {
        list($target, $column) = $this->parseMappedColumn($mapping);

        if (in_array($method, ['pluck', 'value', 'aggregate', 'orderBy', 'lists'])) {
            return $this->mappedJoinQuery($query, $method, $args, $target, $column);
        }

        return $this->mappedHasQuery($query, $method, $args, $target, $column);
    }
750742
public function cursor(Query $query)
    {
        // 分析查询表达式
        $options = $query->getOptions();

        // 生成查询SQL
        $sql = $this->builder->select($query);

        $bind = $query->getBind();

        $condition = isset($options['where']['AND']) ? $options['where']['AND'] : null;
        $relation  = isset($options['relaltion']) ? $options['relation'] : null;

        // 执行查询操作
        return $this->getCursor($sql, $bind, $options['master'], $query->getModel(), $condition, $relation);
    }
593256
public static function addOrderSort($sort, $table, &$query)
    {
        if (!empty($sort)) {
            $sorts = explode(',', $sort);
            foreach ($sorts as $sort) {
                if (!strpos($sort, '.')) {
                    preg_match('/\w+\s+(DESC|ASC)/', $sort, $sort_field);
                    $type = !empty($sort_field) ? trim($sort_field[1]) : 'DESC';
                    $field = !empty($sort_field) ? trim(substr($sort, 0, -strlen($type))) : trim($sort);
                    $order[$table . '.' . $field] = $type == 'DESC' ? SORT_DESC : SORT_ASC;
                } else {
                    $sort_table = trim(substr($sort, 0, strrpos($sort, '.')));
                    preg_match('/\w+\.\w+\s+(DESC|ASC)/', $sort, $sort_field);
                    $type = trim($sort_field[1]);
                    $field = trim(substr(substr($sort, strrpos($sort, '.') + 1), 0, -strlen($type)));
                    $order[trim($sort_table) . '.' . $field] = $type == 'DESC' ? SORT_DESC : SORT_ASC;;
                    $query->select[] = explode(' ', $sort_field[0])[0];
                    $query->joinWith($sort_table);
                }
            }
            $query->select[] = $table . ".*";
        }
        $query->orderBy($order);
    }
1146192
public function select(Query $query)
    {
        $options = $query->getOptions();

        return str_replace(
            ['%TABLE%', '%DISTINCT%', '%FIELD%', '%JOIN%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%LOCK%', '%COMMENT%', '%FORCE%'],
            [
                $this->parseTable($query, $options['table']),
                $this->parseDistinct($query, $options['distinct']),
                $this->parseField($query, $options['field']),
                $this->parseJoin($query, $options['join']),
                $this->parseWhere($query, $options['where']),
                $this->parseGroup($query, $options['group']),
                $this->parseHaving($query, $options['having']),
                $this->parseOrder($query, $options['order']),
                $this->parseLimit($query, $options['limit']),
                $this->parseUnion($query, $options['union']),
                $this->parseLock($query, $options['lock']),
                $this->parseComment($query, $options['comment']),
                $this->parseForce($query, $options['force']),
            ],
            $this->selectSql);
    }
576433
final public function join($table,$column,$operator=null,$value=null, $scape_quote=false) {
      $this->clauseJoin($table,$column,$operator,$value,$scape_quote,"");
      return $this;
   }
473641
public function rawSelectOne(Query $query, $join = 'all')
        {
            $query->setTable($this);
            $query->setupJoinTables($join);
            $query->setLimit(1);
            $query->generateSelectSQL();

            $resultset = Statement::getPreparedStatement($query)->execute();
            $resultset->next();

            return $resultset->getCurrentRow();
        }