yii\db\Query::create PHP Method

create() public static method

The properties being copies are the ones to be used by query builders.
public static create ( Query $from ) : Query
$from Query the source query object
return Query the new Query object
    public static function create($from)
    {
        return new self(['where' => $from->where, 'limit' => $from->limit, 'offset' => $from->offset, 'orderBy' => $from->orderBy, 'indexBy' => $from->indexBy, 'select' => $from->select, 'selectOption' => $from->selectOption, 'distinct' => $from->distinct, 'from' => $from->from, 'groupBy' => $from->groupBy, 'join' => $from->join, 'having' => $from->having, 'union' => $from->union, 'params' => $from->params]);
    }

Usage Example

Example #1
0
 /**
  * @inheritdoc
  */
 public function prepare($builder)
 {
     // NOTE: because the same ActiveQuery may be used to build different SQL statements
     // (e.g. by ActiveDataProvider, one for count query, the other for row data query,
     // it is important to make sure the same ActiveQuery can be used to build SQL statements
     // multiple times.
     if (!empty($this->joinWith)) {
         $this->buildJoinWith();
         $this->joinWith = null;
         // clean it up to avoid issue https://github.com/yiisoft/yii2/issues/2687
     }
     if (empty($this->from)) {
         /* @var $modelClass ActiveRecord */
         $modelClass = $this->modelClass;
         $tableName = $modelClass::tableName();
         $this->from = [$tableName];
     }
     if (empty($this->select) && !empty($this->join)) {
         list(, $alias) = $this->getQueryTableName($this);
         $this->select = ["{$alias}.*"];
     }
     if ($this->primaryModel === null) {
         // eager loading
         $query = Query::create($this);
     } else {
         // lazy loading of a relation
         $where = $this->where;
         if ($this->via instanceof self) {
             // via junction table
             $viaModels = $this->via->findJunctionRows([$this->primaryModel]);
             $this->filterByModels($viaModels);
         } elseif (is_array($this->via)) {
             // via relation
             /* @var $viaQuery ActiveQuery */
             list($viaName, $viaQuery) = $this->via;
             if ($viaQuery->multiple) {
                 $viaModels = $viaQuery->all();
                 $this->primaryModel->populateRelation($viaName, $viaModels);
             } else {
                 $model = $viaQuery->one();
                 $this->primaryModel->populateRelation($viaName, $model);
                 $viaModels = $model === null ? [] : [$model];
             }
             $this->filterByModels($viaModels);
         } else {
             $this->filterByModels([$this->primaryModel]);
         }
         $this->_viaModels = !empty($viaModels) ? $viaModels : [];
         $query = Query::create($this);
         $this->where = $where;
     }
     if (!empty($this->on)) {
         $query->andWhere($this->on);
     }
     return $query;
 }
All Usage Examples Of yii\db\Query::create