yiisoft/yii2 · error · yii\base\InvalidArgumentException

Operator '$operator' requires two operands.

Error message

Operator '$operator' requires two operands.

What it means

yii\db\conditions\InCondition::fromArrayDefinition() (framework/db/conditions/InCondition.php:86) validates IN / NOT IN conditions in operator format and requires both operands — the column (or column list) and the values — to be set. The isset() check means ['in','col',null] throws exactly like ['in','col']. An empty values array is accepted (it builds a harmless false condition); a missing or null one is not.

Source

Thrown at framework/db/conditions/InCondition.php:86

    {
        return $this->column;
    }

    /**
     * @return ExpressionInterface[]|string[]|int[]
     */
    public function getValues()
    {
        return $this->values;
    }
    /**
     * {@inheritdoc}
     * @throws InvalidArgumentException if wrong number of operands have been given.
     */
    public static function fromArrayDefinition($operator, $operands)
    {
        if (!isset($operands[0], $operands[1])) {
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
        }

        return new static($operands[0], $operator, $operands[1]);
    }
}

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Pass both operands: ['in', 'id', $ids]
  2. If the list is optional, skip the condition for null/empty input: if (!empty($ids)) { ... } — andFilterWhere only sanitizes the first operand
  3. Coalesce to an empty array: ['in', 'id', $ids ?? []] builds a false condition instead of throwing

Example fix

// before
$query->andWhere(['in', 'id', $ids]); // $ids === null -> Operator 'IN' requires two operands.

// after
if (!empty($ids)) {
    $query->andWhere(['in', 'id', $ids]);
} // or: $query->andWhere(['in', 'id', $ids ?? []]);
Defensive patterns

Strategy: validation

Validate before calling

// Normalize an optional id list before building the condition
$ids = is_array($ids)
    ? array_values(array_filter($ids, static fn ($v) => $v !== null && $v !== ''))
    : null;

if (!empty($ids)) {
    $query->andWhere(['in', 'id', $ids]);
} // else: omit the condition entirely

Try / catch

try {
    $rows = $query->all();
} catch (\yii\base\InvalidArgumentException $e) {
    \Yii::warning('Malformed IN condition: ' . $e->getMessage(), __METHOD__);
    $rows = [];
}

Prevention

When it happens

Trigger: ->where(['in','id']) with the values omitted; ['not in','status',null] when the id/status list is null; building ['in',$column,$ids] from request data where $ids never arrived.

Common situations: REST endpoints filtering by an optional id list (?ids=) that arrives empty or null; andFilterWhere(['in','id',null]) does NOT save you — filterCondition (framework/db/QueryTrait.php:278-281) only checks operand 1 (the column), so the null values operand survives and throws here; passing null instead of [] when nothing was selected.

Related errors


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/97312b7c48202c3b. Report an issue: GitHub.