[BUG]: PHQL; Error on UPDATE SET col = expression with placeholder
Describe the bug
When using an expression with a placeholder after SET in an UPDATE query, I get the following error: Invalid parameter number: mixed named and positional parameters. Probably because of the SELECT query that is made before the UPDATE query.
A pretty common scenario would be: SET col = col + some_number.
To Reproduce
<?php
class Objects extends \Phalcon\Mvc\Model
{
public $obj_id;
public $obj_name;
public $obj_type;
public function initialize() {
$this->useDynamicUpdate(true);
// $this->skipAttributes(['obj_name']);
}
}
$di = new \Phalcon\Di\FactoryDefault;
$di->set('db', function() {
$db = new \Phalcon\Db\Adapter\Pdo\Mysql([
'dbname' => 'test',
'username' => 'test',
'password' => 'test',
]);
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('db:afterQuery', function($ev, $db) {
echo $db->getSQLStatement() . "\n";
});
$db->setEventsManager($eventsManager);
return $db;
});
try {
/*\Phalcon\Mvc\Model::setup([
'events' => false,
'virtualForeignKeys' => false,
]);*/
$app = new \Phalcon\Mvc\Application($di);
$app->modelsManager->executeQuery(
'UPDATE Objects SET obj_type = obj_type + :obj_type: WHERE obj_id = 1', // err
// 'SET obj_type = obj_type + 2', // ok
// 'SET obj_type = :obj_type:', // ok
[
'obj_type' => 2
]
);
} catch ( Exception $ex ) {
print_r($ex->getMessage() . "\n");
}
Expected behavior
To be able to use placeholders in expressions after SET.
Details
- Phalcon version: 4.0.0-rc.3, Nov 16 2019 18:04:58, Zephir Version 0.12.11-8b9430c
- PHP Version: PHP 7.3.12 (cli) (built: Nov 19 2019 13:57:43) ( NTS MSVC15 (Visual C++ 2017) x64 )
- Operating System: Win 10 x64
- MySQL: 5.7.28
- Server: Nginx
- Other related info (Database, table schema):
CREATE DATABASE IF NOT EXISTS `test`;
USE `test`;
CREATE TABLE IF NOT EXISTS `objects` (
`obj_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`obj_name` varchar(20) NOT NULL,
`obj_type` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`obj_id`)
) ENGINE=InnoDB;
INSERT INTO `objects` (`obj_id`, `obj_name`, `obj_type`) VALUES (1, 'test1', 1);
Need to investigate this. Could be related to phalcon/cphalcon#2373
Yeah, it seems to be the same issue. I forgot to search for the error message...
@Deathamns No problem. It's good that we got our eyes on it again. We will use this issue to track progress.
@sergeyklay Could you have a look at this? I think this handled in the PHQL C part. Not sure if we can make this for 4 otherwise we need to move it to 4.1. See phalcon/cphalcon#2373 as well.
I plan to dig into lexers after 4.0.0 release. So yes, let's add this one to 4.1.x milestone
I found a similar issue related to this. I'm using PHP v8.3.13, Phalcon v5.8.0 and the database is Postgres v14.15.
My PHQL is like this "UPDATE ModelObject SET value = value + 2.00 WHERE id = 68", the value column is numeric type. While the value is 0 (zero) then it's not updating anything but if there's a non-zero value in that field then it's updating. In the SQL statement (using db:afterQuery), I've seen that the value is not considered in the UPDATE query for 0 value but added for non-zero.
Resolved in https://github.com/phalcon/cphalcon/pull/16978
Thank you @Deathamns
Reverting this and reopening. This solution causes more issues downstream. This has to be fixed differently
Related #17045
Resolved in https://github.com/phalcon/cphalcon/pull/17328