[BUG]: `\Phalcon\Mvc\Model\Manager` converts `NOT BETWEEN` operator to `BETWEEN NOT`
Questions? Forum: https://phalcon.io/forum or Discord: https://phalcon.io/discord
Describe the bug
Models manager gets a query with the NOT BETWEEN operation in the WHERE clause, it converts the operator to BETWEEN NOT and receives an error on execution.
The issue stems from \Phalcon\Mvc\Model\Query::getExpression(). It resolves the operator to BETWEEN NOT at phalcon/Mvc/Model/Query.zep#L2063-L2069
To Reproduce
- Create a table with a field compatible with the
BETWEENoperator - Write a query with a
NOT BETWEENoperator in theWHEREclause - Execute the query through the
\Phalcon\Mvc\Model\Managerservice - Check the final query that's been executed and see the switch to
BETWEEN NOT
$q = 'SELECT created_at FROM [Models\Orders] WHERE created_at NOT BETWEEN "2025-10-02 00:00:00" AND "2025-10-02 23:59:59"';
try {
$this->modelsManager->executeQuery($q);
} catch (Exception $e) {
$sql = $this->db->getSQLStatement();
}
The variable $sql now has the operator changed to BETWEEN NOT
Expected behavior
The operator should remain as NOT BETWEEN, BETWEEN NOT is not a valid usage and fails the execution.
Details
- Phalcon version: 4.x, 5.x
- PHP Version: 7.x, 8.x
- Server: Nginx
The root cause of this bug is a string literal typo in phalcon/Mvc/Model/Query.zep at line ~2093.
The PHQL_T_BETWEEN_NOT case outputs "BETWEEN NOT" but it should output "NOT BETWEEN":
case PHQL_T_BETWEEN_NOT:
let exprReturn = [
"type": "binary-op",
- "op": "BETWEEN NOT",
+ "op": "NOT BETWEEN",
"left": left,
"right": right
];
https://github.com/phalcon/cphalcon/blob/b4d4d7e928dcfd287357e4f16d1212e254682061/phalcon/Mvc/Model/Query.zep#L2090-L2098
Notes
- The PHQL scanner/parser already handles
NOT BETWEENcorrectly at the C level viaPHQL_T_BETWEEN_NOT(token 332). The issue is only in howQuery.zepconverts that token to the SQL string. - This fix does not require any changes to the Zephir parser or the PHQL C parser — it is a one-line string fix in
Query.zep. - The related Zephir parser issue (zephir-lang/php-zephir-parser#170) and its PR (zephir-lang/php-zephir-parser#179) add
in/not inas Zephir language expression operators, which is a separate enhancement.
Resolved in https://github.com/phalcon/cphalcon/pull/16929
Thank you @toramanlis