Exemplo n.º 1
0
 /**
  * @param array $attributes
  * @return \yii\db\ActiveQuery
  */
 public function buildQuery($attributes = [])
 {
     $this->setAttributes($attributes);
     if (!empty($this->modelClass) && empty($this->query)) {
         /** @var ActiveRecord $model */
         $model = new $this->modelClass();
         $this->query = $model->find();
     }
     if ($this->validate()) {
         if (!empty($this->query)) {
             $this->filterAttributes();
         }
         if (($sort = $this->getSort()) !== false) {
             $this->query->addOrderBy($sort->getOrders());
         }
         if ($this->whereEnabled && $this->where) {
             $this->query = $this->setWhere($this->query, $this->where);
         }
         return $this->query;
     }
     return null;
 }
 /**
  *
  * Apply current condition will be applied to a query using this function.
  *
  * @param \yii\db\ActiveQuery $query The query object which current condition must be applied to
  */
 public function prepareQuery($query)
 {
     $field = $this->functionObj->prepareSql();
     if (!$field) {
         return NULL;
     }
     switch ($this->operation) {
         case 'select':
             // Here I need to add property to the target class accroding to alias
             $query->addSelect([$this->alias => $field]);
             break;
         case 'where':
             $query->andWhere($field);
             break;
         case 'group':
             $query->addGroupBy($field);
             break;
         case 'order':
             $query->addOrderBy($field);
             break;
         case 'order_inv':
             $query->addOrderBy([$field => SORT_DESC]);
             break;
     }
 }
 public function sort(ActiveQuery $query)
 {
     $query->addOrderBy('sort DESC');
     return $query;
 }