示例#1
0
 public function createQuery($params)
 {
     $query = new Query();
     $fields = $this->extractCommaSeparatedValues($params, 'fields');
     $offset = $this->extractInt($params, 'offset');
     $limit = $this->extractInt($params, 'limit');
     $having = $this->extractArray($params, 'having');
     $where = $this->extractArray($params, 'where');
     $or = $this->extractArray($params, 'or');
     $in = $this->extractArray($params, 'in');
     $sort = $this->extractArray($params, 'sort');
     if ($fields) {
         $query->addManyFields($fields);
     }
     if ($offset) {
         $query->setOffset($offset);
     }
     if ($limit) {
         $query->setLimit($limit);
     }
     if ($having) {
         foreach ($having as $field => $value) {
             $query->addCondition(new Condition(Condition::TYPE_OR, $field, Query::OPERATOR_IS_EQUAL, $value));
         }
     }
     if ($where) {
         foreach ($where as $field => $condition) {
             foreach ($condition as $rawOperator => $value) {
                 $operator = $this->extractOperator($rawOperator);
                 if (!is_null($operator)) {
                     $query->addCondition(new Condition(Condition::TYPE_AND, $field, $operator, $value));
                 }
             }
         }
     }
     if ($or) {
         foreach ($or as $where) {
             foreach ($where as $field => $conditions) {
                 foreach ($conditions as $rawOperator => $value) {
                     $operator = $this->extractOperator($rawOperator);
                     if (!is_null($operator)) {
                         $query->addCondition(new Condition(Condition::TYPE_OR, $field, $operator, $value));
                     }
                 }
             }
         }
     }
     if ($in) {
         foreach ($in as $field => $values) {
             if (!is_array($values)) {
                 continue;
             }
             $query->addCondition(new Condition(Condition::TYPE_AND, $field, Query::OPERATOR_IS_IN, $values));
         }
     }
     if ($sort) {
         foreach ($sort as $field => $rawDirection) {
             $direction = null;
             switch ($rawDirection) {
                 case self::SORT_DESCENDING:
                     $direction = Sorter::DESCENDING;
                     break;
                 case self::SORT_ASCENDING:
                 default:
                     $direction = Sorter::ASCENDING;
                     break;
             }
             $query->addSorter(new Sorter($field, $direction));
         }
     }
     return $query;
 }
示例#2
0
 public function merge(Query $query)
 {
     if ($query->hasModel()) {
         $this->setModel($query->getModel());
     }
     if ($query->hasFields()) {
         $this->addManyFields($query->getFields());
     }
     if ($query->hasOffset()) {
         $this->setOffset($query->getOffset());
     }
     if ($query->hasLimit()) {
         $this->setLimit($query->getLimit());
     }
     if ($query->hasConditions()) {
         $this->addManyConditions($query->getConditions());
     }
     if ($query->hasSorters()) {
         $this->addManySorters($query->getSorters());
     }
     return $this;
 }