The default implementation of this interface is provided by QueryTrait. It has support for getting [[one]] instance or [[all]]. Allows pagination via [[limit]] and [[offset]]. Sorting is supported via [[orderBy]] and items can be limited to match some conditions using [[where]].
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Author: Carsten Brandt (mail@cebe.cc)
Beispiel #1
0
 /**
  * Ajoute à $query les clauses de tri stockées en session
  *
  * @param QueryInterface $query
  * @param string         $sessionKey La clé de session, en principe au format : app\models\MaClasse.sort
  * @return QueryInterface
  */
 public static function updateQuery(QueryInterface $query, $sessionKey)
 {
     // La clé de session contient un tableau au format : nom_colonne => ordre_de_tri
     $sortClauses = Yii::$app->session->get($sessionKey, []);
     foreach ($sortClauses as $field => $clause) {
         if ($clause == 'asc' || $clause == 'desc') {
             $query->addOrderBy($field . ' ' . $clause);
         }
     }
     return $query;
 }
 /**
  * Set the page-from-pk option
  * @param QueryInterface $query
  */
 protected function setPageFrom(QueryInterface $query)
 {
     if ($this->pageFromPk) {
         $class = $this->query->modelClass;
         $pks = $class::primaryKey();
         if (count($pks) > 1) {
             throw new NotSupportedException('The "page-from-pk" filter can not be apply for composite primary key.');
         }
         $query->andWhere(['<', $pks[0], $this->pageFromPk]);
     }
     return $query;
 }
Beispiel #3
0
 /**
  * @param \yii\db\QueryInterface $query
  * @param array $params
  * @return \yii\db\QueryInterface
  */
 public function refine($query, $params)
 {
     $where = ['or'];
     $bind = [];
     foreach ($params as $key => $val) {
         $name = ":{$this->name}_{$key}";
         $where[] = "{$this->columnName} LIKE {$name}";
         $bind[$name] = $val . '%';
     }
     $query->andWhere($where, $bind);
     return parent::refine($query, $params);
 }
 /**
  * Adds primary key filter/s to the Query object.
  * 
  * @param QueryInterface $query
  * @return QueryInterface
  */
 protected function addPrimaryKeyCriteria(QueryInterface $query)
 {
     $class = get_class($this->getOwner());
     $pks = (array) $class::primaryKey();
     foreach ($pks as $pk) {
         $query->andWhere([$pk => $this->getOwner()->{$pk}]);
     }
     return $query;
 }
 /**
  * 
  * @param QueryInterface $query
  * @param string $relationName
  * @param string $relationTableName defaults to null (use when you create alias)
  * @param string $relationTableAlias defaults to null (use when you create alias)
  */
 public function joinWithRelation(&$query, $relationName, $relationTableName = null, $relationTableAlias = null)
 {
     if (empty($relationTableAlias)) {
         $query->joinWith([$relationName]);
     } else {
         $query->joinWith([$relationName => function ($query) use($relationTableAlias, $relationTableName) {
             $query->from([$relationTableAlias => $relationTableName]);
         }]);
     }
 }
Beispiel #6
0
 /**
  * @param QueryInterface $query
  * @param array $filter
  */
 public function defaultFilter($query, $filter)
 {
     $field = $filter['field'];
     if (isset($this->fieldMap[$field])) {
         $field = $this->fieldMap[$field];
     }
     $op = isset($filter['op']) ? $filter['op'] : 'contains';
     $value = isset($filter['value']) ? $filter['value'] : '';
     if ($value === '' && $op != 'equal') {
         return;
     }
     switch ($op) {
         case 'contains':
             $query->andFilterWhere([$field => $value]);
             break;
         case 'equal':
             $query->andWhere([$field => $value]);
             break;
         case 'notequal':
             $query->andWhere(['<>', $field, $value]);
             break;
         case 'beginwith':
             $query->andWhere(['like', $field, $value . '%']);
             break;
         case 'endwith':
             $query->andWhere(['like', $field, '%' . $value]);
             break;
         case 'less':
             $query->andWhere(['<', $field, $value]);
             break;
         case 'lessorequal':
             $query->andWhere(['<=', $field, $value]);
             break;
         case 'greater':
             $query->andWhere(['>', $field, $value]);
             break;
         case 'greaterorequal':
             $query->andWhere(['>=', $field, $value]);
             break;
     }
 }
Beispiel #7
0
 private function filterByInfractionDate(QueryInterface &$query, $filter, $days_ago)
 {
     if (!array_key_exists($filter, $this->getCreatedAtFilters())) {
         return false;
     }
     switch ($filter) {
         case self::CREATED_AT_TODAY:
             return $query->andFilterWhere(['>', 'record.created_at', strtotime('midnight')]);
         case self::CREATED_AT_LAST_3_DAYS:
             return $query->andFilterWhere(['>', 'record.created_at', strtotime('-3 days')]);
         case self::CREATED_AT_LAST_X_DAYS:
             if (!is_numeric($days_ago) || $days_ago <= 0 || $days_ago > 366) {
                 return false;
             }
             return $query->andFilterWhere(['>', 'record.created_at', strtotime('-' . $days_ago . ' days')]);
     }
 }
Beispiel #8
0
 /**
  * Default filter for search
  * @param QueryInterface $query
  * */
 public function filters($query)
 {
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'id', $this->id]);
 }