yiisoft/yii2 · error · yii\base\InvalidArgumentException
Invalid operator '$operator'.
Error message
Invalid operator '$operator'.
What it means
yii\db\conditions\LikeConditionBuilder::parseOperator() (framework/db/conditions/LikeConditionBuilder.php:109) decomposes a LikeCondition's operator into conjunction (AND/OR), negation, and LIKE/ILIKE using the regex /^(AND |OR |)(((NOT |))I?LIKE)/. Anything not matching — an optional 'AND '/'OR ' prefix, optional 'NOT ', then exactly 'LIKE' or 'ILIKE' with single spaces — is rejected with InvalidArgumentException. Operators routed through conditionClasses ('LIKE','NOT LIKE','OR LIKE','OR NOT LIKE', pgsql 'ILIKE' variants at framework/db/pgsql/QueryBuilder.php:86-89) always match, so this error comes from LikeCondition objects constructed directly, or from a custom conditionClasses alias whose string does not follow the pattern.
Source
Thrown at framework/db/conditions/LikeConditionBuilder.php:109
* @return string
*/
private function getEscapeSql()
{
if ($this->escapeCharacter !== null) {
return " ESCAPE '{$this->escapeCharacter}'";
}
return '';
}
/**
* @param string $operator
* @return array
*/
protected function parseOperator($operator)
{
if (!preg_match('/^(AND |OR |)(((NOT |))I?LIKE)/', $operator, $matches)) {
throw new InvalidArgumentException("Invalid operator '$operator'.");
}
$andor = ' ' . (!empty($matches[1]) ? $matches[1] : 'AND ');
$not = !empty($matches[3]);
$operator = $matches[2];
return [$andor, $not, $operator];
}
}
View on GitHub (pinned to 66f00d18a2)
Solutions
- Use only the recognized operators: LIKE, NOT LIKE, OR LIKE, OR NOT LIKE (plus ILIKE variants on PostgreSQL)
- For genuinely custom SQL operators use \yii\db\Expression with bound params instead of LikeCondition
- If you register an alias in conditionClasses, make its string match the regex exactly (e.g. 'OR NOT ILIKE') or map it to a custom condition class with its own builder
Example fix
// before
$cond = new \yii\db\conditions\LikeCondition('name', 'MATCH', 'foo'); // Invalid operator 'MATCH'
// after
$cond = new \yii\db\conditions\LikeCondition('name', 'LIKE', 'foo');
// or for custom predicates:
$cond = new \yii\db\Expression('name ~ :pat', [':pat' => '^foo']); Defensive patterns
Strategy: validation
Validate before calling
function isValidLikeOperator(string $operator): bool
{
return (bool) preg_match('/^(AND |OR |)(((NOT |))I?LIKE)/', $operator);
}
if (!isValidLikeOperator($op)) {
throw new \InvalidArgumentException("Unsupported LIKE operator: $op");
}
$cond = new \yii\db\conditions\LikeCondition($column, $op, $value); Try / catch
try {
$sql = $queryBuilder->buildExpression($condition, $params);
} catch (\yii\base\InvalidArgumentException $e) {
// operator rejected by parseOperator — fall back to a raw Expression
$sql = $queryBuilder->buildExpression(new \yii\db\Expression($rawSql, $params), $params);
} Prevention
- Whitelist operators instead of building them from strings: in_array($op, ['LIKE','NOT LIKE','OR LIKE','OR NOT ILIKE'], true)
- Use the array format ['like', $col, $val] — it routes through conditionClasses and cannot hit parseOperator with garbage
- For custom predicates implement a dedicated condition class + builder instead of reusing LikeCondition
When it happens
Trigger: new LikeCondition('name', 'SIMILAR TO', 'foo'); registering a custom alias via setConditionClasses(['MATCH' => LikeCondition::class]) and using ['match','name','x']; hand-built operator strings like 'NOT LIKE' (double space) or 'like it'.
Common situations: Code written before Yii 2.0.14 (when condition building moved to dedicated classes) that built LIKE operators manually; porting PostgreSQL-specific search operators; reusable query helper packages constructing LikeCondition instances.
Related errors
- Operator '$operator' requires two operands.
- Operator '$operator' requires two operands.
- Operator '$operator' requires three operands.
- Subquery for EXISTS operator must be a Query object.
- Operator '$operator' requires two operands.
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/b995384af7f0ab2e.
Report an issue: GitHub.