yii\db\QueryBuilder::buildInCondition PHP Method

buildInCondition() public method

Creates an SQL expressions with the IN operator.
public buildInCondition ( string $operator, array $operands, array &$params ) : string
$operator string the operator to use (e.g. `IN` or `NOT IN`)
$operands array the first operand is the column name. If it is an array a composite IN condition will be generated. The second operand is an array of values that column value should be among. If it is an empty array the generated expression will be a `false` value if operator is `IN` and empty if operator is `NOT IN`.
$params array the binding parameters to be populated
return string the generated SQL expression
    public function buildInCondition($operator, $operands, &$params)
    {
        if (!isset($operands[0], $operands[1])) {
            throw new Exception("Operator '{$operator}' requires two operands.");
        }
        list($column, $values) = $operands;
        if ($column === []) {
            // no columns to test against
            return $operator === 'IN' ? '0=1' : '';
        }
        if ($values instanceof Query) {
            return $this->buildSubqueryInCondition($operator, $column, $values, $params);
        }
        if (!is_array($values) && !$values instanceof \Traversable) {
            // ensure values is an array
            $values = (array) $values;
        }
        if ($column instanceof \Traversable || count($column) > 1) {
            return $this->buildCompositeInCondition($operator, $column, $values, $params);
        } elseif (is_array($column)) {
            $column = reset($column);
        }
        $sqlValues = [];
        foreach ($values as $i => $value) {
            if (is_array($value) || $value instanceof \ArrayAccess) {
                $value = isset($value[$column]) ? $value[$column] : null;
            }
            if ($value === null) {
                $sqlValues[$i] = 'NULL';
            } elseif ($value instanceof Expression) {
                $sqlValues[$i] = $value->expression;
                foreach ($value->params as $n => $v) {
                    $params[$n] = $v;
                }
            } else {
                $phName = self::PARAM_PREFIX . count($params);
                $params[$phName] = $value;
                $sqlValues[$i] = $phName;
            }
        }
        if (empty($sqlValues)) {
            return $operator === 'IN' ? '0=1' : '';
        }
        if (strpos($column, '(') === false) {
            $column = $this->db->quoteColumnName($column);
        }
        if (count($sqlValues) > 1) {
            return "{$column} {$operator} (" . implode(', ', $sqlValues) . ')';
        } else {
            $operator = $operator === 'IN' ? '=' : '<>';
            return $column . $operator . reset($sqlValues);
        }
    }