yii\elasticsearch\QueryBuilder::buildCondition PHP Method

buildCondition() public method

Parses the condition specification and generates the corresponding SQL expression.
public buildCondition ( string | array $condition ) : string
$condition string | array the condition specification. Please refer to [[Query::where()]] on how to specify a condition.
return string the generated SQL expression
    public function buildCondition($condition)
    {
        static $builders = ['not' => 'buildNotCondition', 'and' => 'buildAndCondition', 'or' => 'buildAndCondition', 'between' => 'buildBetweenCondition', 'not between' => 'buildBetweenCondition', 'in' => 'buildInCondition', 'not in' => 'buildInCondition', 'like' => 'buildLikeCondition', 'not like' => 'buildLikeCondition', 'or like' => 'buildLikeCondition', 'or not like' => 'buildLikeCondition', 'lt' => 'buildHalfBoundedRangeCondition', '<' => 'buildHalfBoundedRangeCondition', 'lte' => 'buildHalfBoundedRangeCondition', '<=' => 'buildHalfBoundedRangeCondition', 'gt' => 'buildHalfBoundedRangeCondition', '>' => 'buildHalfBoundedRangeCondition', 'gte' => 'buildHalfBoundedRangeCondition', '>=' => 'buildHalfBoundedRangeCondition'];
        if (empty($condition)) {
            return [];
        }
        if (!is_array($condition)) {
            throw new NotSupportedException('String conditions in where() are not supported by elasticsearch.');
        }
        if (isset($condition[0])) {
            // operator format: operator, operand 1, operand 2, ...
            $operator = strtolower($condition[0]);
            if (isset($builders[$operator])) {
                $method = $builders[$operator];
                array_shift($condition);
                return $this->{$method}($operator, $condition);
            } else {
                throw new InvalidParamException('Found unknown operator in query: ' . $operator);
            }
        } else {
            // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
            return $this->buildHashCondition($condition);
        }
    }

Usage Example

コード例 #1
0
 /**
  * Parses the condition specification and generates the corresponding SQL expression.
  *
  * @param string|array $condition the condition specification. Please refer to [[Query::where()]] on how to specify a condition.
  * @throws \yii\base\InvalidParamException if unknown operator is used in query
  * @throws \yii\base\NotSupportedException if string conditions are used in where
  * @return string the generated SQL expression
  */
 public function buildCondition($condition)
 {
     if ($condition instanceof Param) {
         return $condition->toArray();
     }
     return parent::buildCondition($condition);
 }