cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

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

Open Deathamns opened this issue 7 years ago • 9 comments

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

Deathamns avatar Dec 01 '19 10:12 Deathamns

Need to investigate this. Could be related to phalcon/cphalcon#2373

ruudboon avatar Dec 01 '19 10:12 ruudboon

Yeah, it seems to be the same issue. I forgot to search for the error message...

Deathamns avatar Dec 01 '19 11:12 Deathamns

@Deathamns No problem. It's good that we got our eyes on it again. We will use this issue to track progress.

ruudboon avatar Dec 01 '19 12:12 ruudboon

@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.

ruudboon avatar Dec 04 '19 17:12 ruudboon

I plan to dig into lexers after 4.0.0 release. So yes, let's add this one to 4.1.x milestone

sergeyklay avatar Dec 04 '19 18:12 sergeyklay

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.

kunalray1993 avatar Dec 31 '24 12:12 kunalray1993

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

Thank you @Deathamns

niden avatar May 04 '26 23:05 niden

Reverting this and reopening. This solution causes more issues downstream. This has to be fixed differently

Related #17045

niden avatar May 23 '26 13:05 niden

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

niden avatar Jul 14 '26 01:07 niden