{"record":{"id":"84e583108f408fd7","repo":"yiisoft/yii2","slug":"operator-operator-requires-two-operands-84e583","errorCode":null,"errorMessage":"Operator '$operator' requires two operands.","messagePattern":"Operator '\\$operator' requires two operands\\.","errorType":"exception","errorClass":"yii\\base\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"framework/db/conditions/LikeCondition.php","lineNumber":70,"sourceCode":"        $this->escapingReplacements = $escapingReplacements;\n    }\n\n    /**\n     * @return array|null|false\n     */\n    public function getEscapingReplacements()\n    {\n        return $this->escapingReplacements;\n    }\n\n    /**\n     * {@inheritdoc}\n     * @throws InvalidArgumentException if wrong number of operands have been given.\n     */\n    public static function fromArrayDefinition($operator, $operands)\n    {\n        if (!isset($operands[0], $operands[1])) {\n            throw new InvalidArgumentException(\"Operator '$operator' requires two operands.\");\n        }\n\n        $condition = new static($operands[0], $operator, $operands[1]);\n        if (isset($operands[2])) {\n            $condition->escapingReplacements = $operands[2];\n        }\n\n        return $condition;\n    }\n}\n","sourceCodeStart":52,"sourceCodeEnd":81,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/conditions/LikeCondition.php#L52-L81","documentation":"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.","triggerScenarios":"->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.","commonSituations":"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.","solutions":["Provide both operands: ['like', 'name', $q]","Skip the condition for empty input: if ($q !== null && $q !== '') { $query->andWhere(['like','name',$q]); }","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"],"exampleFix":"// before\n$query->andWhere(['like', 'name', $q]); // $q === null -> Operator 'LIKE' requires two operands.\n\n// after\nif ($q !== null && $q !== '') {\n    $query->andWhere(['like', 'name', $q]);\n}","handlingStrategy":"validation","validationCode":"function hasLikeOperands(array $condition): bool\n{\n    return isset($condition[1], $condition[2]) && $condition[2] !== null;\n}\n\n$q = trim((string) $request->get('q'));\nif ($q !== '') {\n    $query->andWhere(['like', 'name', $q]);\n}","typeGuard":null,"tryCatchPattern":"try {\n    $rows = $query->all();\n} catch (\\yii\\base\\InvalidArgumentException $e) {\n    \\Yii::warning('Malformed LIKE condition: ' . $e->getMessage(), __METHOD__);\n    $rows = [];\n}","preventionTips":["Cast and trim search input: $q = trim((string) $q); skip the condition when empty","andFilterWhere only drops the condition when operand 1 is empty — a null value operand still throws here","Arrays of terms are valid for LIKE; null never is"],"tags":["yii2","database","query-builder","like","where-condition","validation"],"backgroundTag":"malformed-query-condition","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}