phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.77k stars 1.97k forks source link

[BUG]: RawValue not working anymore #16350

Open FaimMedia opened 1 year ago

FaimMedia commented 1 year ago

Questions? Forum: https://phalcon.io/forum or Discord: https://phalcon.io/discord

Describe the bug A clear and concise description of what the bug is.

To Reproduce

Provide output if related. Provide coredump if any. Use https://docs.phalcon.io/en/latest/generating-backtrace as reference.

Steps to reproduce the behavior:

use Phalcon\Db\RawValue;

CustomerPlanModel::findFirst([
    'conditions' => 'dateStart <= :date1: AND dateEnd > :date2:',
    'bind'       => [
        'date1' => new RawValue('CURDATE()'),
        'date2' => new RawValue('CURDATE()'),
    ],
]);
echo $this->getDi()->getShared('db')->getRealSQLStatement();

PHQL, which looks okay:

SELECT ... FROM `customer_plan`
WHERE `customer_plan`.`date_start` <= CURDATE() AND `customer_plan`.`date_end`> CURDATE() LIMIT 1

MySQL query log, the real query that gets executed (notice the quotes around CURDATE()):

SELECT ... FROM `customer_plan` WHERE `customer_plan`.`date_start` <= 'CURDATE()' AND `customer_plan`.`date_end` > 'CURDATE()' LIMIT 1

Details

s-ohnishi commented 4 months ago

Since null is not recognized as a reserved word in SQL, no one can set a certain column to NULL. I think this is a serious bug.

yahveh commented 4 months ago

is there any temporary solution or idea available?

s-ohnishi commented 4 months ago

@yahveh I don't think there is any way to avoid this if you use Phalcon's SQL builder. I think you can get an instance of the PDO class by getting db from the DI container. I think the only way is to write it in plain PHP using SQL that you have constructed yourself with that. This is a way to avoid the bug that causes part of the column name to be mistaken for a reserved word, but it means you cannot use the functions provided in the Models class, so I think it is very inconvenient.