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
``sql
CREATE DATABASE IF NOT EXISTStest;
USEtest`;
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 #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 #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
Most helpful comment
I plan to dig into lexers after 4.0.0 release. So yes, let's add this one to 4.1.x milestone