yiisoft/yii2 · error · yii\base\InvalidArgumentException

Operator '$operator' requires two operands.

Error message

Operator '$operator' requires two operands.

What it means

yii\db\conditions\SimpleCondition::fromArrayDefinition() (framework/db/conditions/SimpleCondition.php:81) handles binary operators (=, !=, <>, >, <, >=, <=) in operator format and requires count($operands) === 2 — column and value, no more, no less. SimpleCondition is also the fallback for ANY operator string not present in QueryBuilder::$conditionClasses (framework/db/QueryBuilder.php:1604-1607), so unknown operators with a wrong operand count surface here too. Because it uses count() rather than isset(), a null value is accepted — ['=','col',null] passes this check and later builds broken SQL, so NULL comparisons must use the dedicated is/is not operators.

Source

Thrown at framework/db/conditions/SimpleCondition.php:81

        return $this->column;
    }

    /**
     * @return mixed
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * {@inheritdoc}
     * @throws InvalidArgumentException if wrong number of operands have been given.
     */
    public static function fromArrayDefinition($operator, $operands)
    {
        if (count($operands) !== 2) {
            throw new InvalidArgumentException("Operator '$operator' requires two operands.");
        }

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

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Supply exactly two operands: ['>', 'age', 18]
  2. For NULL comparisons use the dedicated operators: ['is', 'col', null] / ['is not', 'col', null] instead of ['=', 'col', null]
  3. Build operator conditions in one step — [$op, $column, $value] — rather than pushing pieces separately

Example fix

// before
$query->where(['=', 'deleted_at', null]); // passes count check but yields 'deleted_at = NULL' (never true)

// after
$query->where(['is', 'deleted_at', null]);
Defensive patterns

Strategy: validation

Validate before calling

function isValidSimpleCondition(array $condition): bool
{
    return isset($condition[0], $condition[1]) && count($condition) === 3;
}

if (isValidSimpleCondition([$op, $column, $value])) {
    $query->andWhere([$op, $column, $value]);
}

Try / catch

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

Prevention

When it happens

Trigger: ->where(['>','age']) missing the value; ['='] alone; ['<=','price',10,'extra'] with a stray third element; an operator typo like '==' still routes here (unknown operator + 2 operands builds invalid SQL that fails at the DB instead).

Common situations: Dynamic filters where the value variable is unset or the filter row was partially built; concatenating condition arrays with off-by-one pushes; mixing hash and operator formats in one array.

Related errors


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