yii\db\QueryBuilder::build PHP Method

build() public method

Generates a SELECT SQL statement from a Query object.
public build ( Query $query, array $params = [] ) : array
$query Query the [[Query]] object from which the SQL statement will be generated.
$params array the parameters to be bound to the generated SQL statement. These parameters will be included in the result with the additional parameters generated during the query building process.
return array the generated SQL statement (the first array element) and the corresponding parameters to be bound to the SQL statement (the second array element). The parameters returned include those provided in `$params`.
    public function build($query, $params = [])
    {
        $query = $query->prepare($this);
        $params = empty($params) ? $query->params : array_merge($params, $query->params);
        $clauses = [$this->buildSelect($query->select, $params, $query->distinct, $query->selectOption), $this->buildFrom($query->from, $params), $this->buildJoin($query->join, $params), $this->buildWhere($query->where, $params), $this->buildGroupBy($query->groupBy), $this->buildHaving($query->having, $params)];
        $sql = implode($this->separator, array_filter($clauses));
        $sql = $this->buildOrderByAndLimit($sql, $query->orderBy, $query->limit, $query->offset);
        if (!empty($query->orderBy)) {
            foreach ($query->orderBy as $expression) {
                if ($expression instanceof Expression) {
                    $params = array_merge($params, $expression->params);
                }
            }
        }
        if (!empty($query->groupBy)) {
            foreach ($query->groupBy as $expression) {
                if ($expression instanceof Expression) {
                    $params = array_merge($params, $expression->params);
                }
            }
        }
        $union = $this->buildUnion($query->union, $params);
        if ($union !== '') {
            $sql = "({$sql}){$this->separator}{$union}";
        }
        return [$sql, $params];
    }

Usage Example

 /**
  * @inheritdoc
  * Firebird has its own SELECT syntax
  * SELECT [FIRST (<int-expr>)] [SKIP (<int-expr>)] <columns> FROM ...
  * @author [email protected]
  */
 public function build($query, $params = [])
 {
     list($sql, $params) = parent::build($query, $params);
     if ($this->hasLimit($query->limit) and $this->hasOffset($query->offset)) {
         $sql = preg_replace('/limit\\s\\d+/i', '', $sql, 1);
         $sql = preg_replace('/offset\\s\\d+/i', '', $sql, 1);
         $sql = preg_replace('/^SELECT /i', "SELECT FIRST {$query->limit} SKIP {$query->offset} ", $sql, 1);
     } elseif ($this->hasLimit($query->limit)) {
         $sql = preg_replace('/limit\\s\\d+/i', '', $sql, 1);
         $sql = preg_replace('/offset\\s\\d+/i', '', $sql, 1);
         $sql = preg_replace('/^SELECT /i', "SELECT FIRST {$query->limit} ", $sql, 1);
     } elseif ($this->hasOffset($query->offset)) {
         $sql = preg_replace('/limit\\s\\d+/i', '', $sql, 1);
         $sql = preg_replace('/offset\\s\\d+/i', '', $sql, 1);
         $sql = preg_replace('/^SELECT /i', "SELECT SKIP {$query->offset} ", $sql, 1);
     }
     return [$sql, $params];
 }