yiisoft/yii2 · error · yii\base\InvalidArgumentException

Operator '$operator' requires two operands.

Error message

Operator '$operator' requires two operands.

What it means

yii\db\conditions\LikeCondition::fromArrayDefinition() (framework/db/conditions/LikeCondition.php:70) validates LIKE-family conditions in operator format (like, not like, or like, or not like, plus ILIKE variants on PostgreSQL) and requires the column and the search value to be set. A third optional operand (escaping on/off, per QueryInterface docs framework/db/QueryInterface.php:137) is accepted when present. Because the guard is isset(), a null search value throws exactly like a missing one.

Source

Thrown at framework/db/conditions/LikeCondition.php:70

        $this->escapingReplacements = $escapingReplacements;
    }

    /**
     * @return array|null|false
     */
    public function getEscapingReplacements()
    {
        return $this->escapingReplacements;
    }

    /**
     * {@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.");
        }

        $condition = new static($operands[0], $operator, $operands[1]);
        if (isset($operands[2])) {
            $condition->escapingReplacements = $operands[2];
        }

        return $condition;
    }
}

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Provide both operands: ['like', 'name', $q]
  2. Skip the condition for empty input: if ($q !== null && $q !== '') { $query->andWhere(['like','name',$q]); }
  3. Use ->andFilterWhere() only with a value that filterCondition can drop via operand 1 — i.e. restructure so the empty-able value sits where the filter looks, or pre-normalize $q

Example fix

// before
$query->andWhere(['like', 'name', $q]); // $q === null -> Operator 'LIKE' requires two operands.

// after
if ($q !== null && $q !== '') {
    $query->andWhere(['like', 'name', $q]);
}
Defensive patterns

Strategy: validation

Validate before calling

function hasLikeOperands(array $condition): bool
{
    return isset($condition[1], $condition[2]) && $condition[2] !== null;
}

$q = trim((string) $request->get('q'));
if ($q !== '') {
    $query->andWhere(['like', 'name', $q]);
}

Try / catch

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

Prevention

When it happens

Trigger: ->where(['like','name']) with no search term; ['or like','title',null] when the search input is null; search forms where the q parameter is absent so $q stays null.

Common situations: Search boxes with optional input; andFilterWhere(['like','name',null]) does not help — filterCondition (framework/db/QueryTrait.php:278-281) only empties the condition when operand 1 (the column) is empty, so a null value operand reaches the builder and throws; passing an array of terms is legal (each term gets its own LIKE), null is not.

Related errors


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