cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: `\Phalcon\Mvc\Model\Manager` converts `NOT BETWEEN` operator to `BETWEEN NOT`

Open toramanlis opened this issue 1 year ago • 2 comments

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 BETWEEN operator
  • Write a query with a NOT BETWEEN operator in the WHERE clause
  • Execute the query through the \Phalcon\Mvc\Model\Manager service
  • 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

toramanlis avatar Oct 02 '25 11:10 toramanlis

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 BETWEEN correctly at the C level via PHQL_T_BETWEEN_NOT (token 332). The issue is only in how Query.zep converts 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 in as Zephir language expression operators, which is a separate enhancement.

Jeckerson avatar Apr 02 '26 23:04 Jeckerson

Resolved in https://github.com/phalcon/cphalcon/pull/16929

Thank you @toramanlis

niden avatar Apr 28 '26 23:04 niden