Cphalcon: [BUG]: PHQL; Error on UPDATE SET col = expression with placeholder

Created on 1 Dec 2019  路  5Comments  路  Source: phalcon/cphalcon

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):

``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);
```

5.0 bug low transfer

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

All 5 comments

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

Was this page helpful?
0 / 5 - 0 ratings