cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Issue with {some:array} in PHQL/Models

Open borisdelev opened this issue 10 years ago • 9 comments

Hi there,

It seems i use that type type of variables much more... so i found issue. Let give u example:

$s = [1 => 2, 2 => 12]; // Where 2 and 12 is some ids
$objs = \Models\ArticleAuthors::find([
 'conditions' => 'article_id={article:int} AND author_id IN ({authors:array})',
 'bind' => ['article' => (int)$id, 'authors' => $s]
]);
  • so this will return error _"PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined"_. After some time of attempts i realize the problem is the keys of the array $s- they did not start from 0! If you make $s = array_values($s); to reset keys all will works fine.

I think this is good to be fixed.

(i hope you did not forget that related issue too: https://github.com/phalcon/cphalcon/issues/11605)

My Phalcon version : 2.1.0r / Mar 23 2016 00:33:33 / on PHP 5.6.3 Windows

Thank You! May the force be with you. Always

borisdelev avatar May 29 '16 19:05 borisdelev

This might be PDO issue more than Phalcon's, since you're getting plain PDOException. Bound parameters should be numerical array (starting at index 0 by default).

stamster avatar May 30 '16 11:05 stamster

Hmm, will accept that... but... is it possible PHQL prepare that or this is on purpose?

borisdelev avatar May 30 '16 11:05 borisdelev

Im guessing this is on purpose. This could be fixed, but it would doing same stuff like array_values. And there is no really convenient way in php(or zephir) to check if assoc array is numerated from 1 or 0 etc. And even if there is one - it will add not needed overhead for this thing.

Jurigag avatar May 31 '16 12:05 Jurigag

Well, something like

if (isset($array[0])) //do something
else //don't do

You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name twice in a prepared statement. You cannot bind multiple values to a single named parameter in, for example, the IN() clause of an SQL statement.

http://php.net/manual/en/pdo.prepare.php

stamster avatar May 31 '16 12:05 stamster

Well, but isn't this stuff only in phalcon ? As far as i know PDO don't allow binding array like we have and phalcon just builds something like (?1, ?2, ?3) ?

Jurigag avatar May 31 '16 12:05 Jurigag

You can bind in PDO like

SELECT * FROM table WHERE id = :id
$bind = ['id' => 1];

As far as I know phalcon doing the same just a bit smarter and required the param to be :id: instead of :id

hisorange avatar Jun 01 '16 10:06 hisorange

I know you can bind in PDO like this. I mean we can't bind array value in PDO..... read first, then answer.

Jurigag avatar Jun 01 '16 10:06 Jurigag

This might be a dupe of #11605

dschissler avatar Jun 04 '16 15:06 dschissler

Okay then, i will close it.

borisdelev avatar Jun 07 '16 08:06 borisdelev