/** * Build between query by two search fields * @param ActiveQuery $query Query object * @param mixed $field Field name: string || array * @param string $format Format name * @param string $to_suffix To field suffix */ public function addRangeCondition($query, $field = 'timestamp', $format = 'timestamp', $to_suffix = '_to') { $model = $this->owner; $search = $field; if (is_array($field)) { $search = key($field); $field = reset($field); } if ($model->{$search}) { $query->andWhere(['>=', $field, $this->formatField($model->{$search}, $format)]); } $search_to = $search . $to_suffix; if ($model->{$search_to}) { $to = $this->formatField($model->{$search_to}, $format); if ($format == 'timestamp') { $to += SECONDS_IN_DAY - 1; } $query->andWhere(['<=', $field, $to]); } }
public function byCluster($cluster) { return parent::andWhere(['cluster' => $cluster]); }
public function one($db = null) { return parent::one($db); }
/** * Creates a DB command that can be used to execute this query. * @param Connection $db the DB connection used to create the DB command. * If null, the DB connection returned by [[modelClass]] will be used. * @return Command the created DB command instance. */ public function createCommand($db = null) { if ($this->primaryModel === null) { // eager loading if (!empty($this->on)) { $where = $this->where; $this->andWhere($this->on); $command = parent::createCommand($db); $this->where = $where; return $command; } else { return parent::createCommand($db); } } // lazy loading $where = $this->where; if ($this->via instanceof self) { // via pivot table $viaModels = $this->via->findPivotRows([$this->primaryModel]); $this->filterByModels($viaModels); } elseif (is_array($this->via)) { // via relation /** @var ActiveRelation $viaQuery */ 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]); } if (!empty($this->on)) { $this->andWhere($this->on); } $command = parent::createCommand($db); $this->where = $where; return $command; }
/** * @param ActiveQuery $query * @param array|string $flags * @param bool $prefixTablename * @return ActiveQuery */ public function addFlagsCriteria($query, $flags, $prefixTablename = false) { if (!is_array($flags)) { $flags = [$flags]; } $trueFlags = []; $falseFlags = []; $table = $prefixTablename ? $this->owner->tableName() . '.' : ''; $class = (new \ReflectionClass($this->owner))->getShortName(); foreach ($flags as $key => $value) { if (isset($this->attributes[$key]) && (bool) $value === false) { $falseFlags[] = $this->attributes[$key]; } else { if (isset($this->attributes[$key])) { $trueFlags[] = $this->attributes[$key]; } else { $trueFlags[] = $this->attributes[$value]; } } } if (!empty($trueFlags)) { $tvalue = $this->mergeFlags($trueFlags); $query->andWhere($table . "[[{$this->flagsAttribute}]] & :tvalue{$class}", [":tvalue{$class}" => $tvalue]); } if (!empty($falseFlags)) { $fvalue = $this->mergeFlags($falseFlags); $query->andWhere('!(' . $table . "[[{$this->flagsAttribute}]] & :fvalue{$class})", [":fvalue{$class}" => $fvalue]); } return $query; }
/** * Add sort for foreign key attribute * If you override this method, remember to call parent implementation. * @param ActiveQuery $query search query * @param ActiveDataProvider $dataProvider data results */ public function addSort($query, $dataProvider) { if ($this->modelName !== null && !$this->oneToMany) { $query->joinWith($this->relationName); $relationClass = $this->relationClass; $dataProvider->sort->attributes[$this->relationParameter] = ['asc' => [$relationClass::tableName() . '.' . $this->modelName => SORT_ASC], 'desc' => [$relationClass::tableName() . '.' . $this->modelName => SORT_DESC]]; } }
public static function find() { return Yii::createObject(ActiveQuery::className(), [get_called_class()]); }