Quantcast

ZF2 - Zend\Db and IN (1,2,3,4,5) only printing out IN (1)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

ZF2 - Zend\Db and IN (1,2,3,4,5) only printing out IN (1)

intellix
This post was updated on .
Hey, just posting here as I'm not 100% where to put this. Opened an issue in github but will post here as well for anyone else having the same issue.

It seems if you pass in an array of values to where->in('id', $array); then it only prints out the first value within the array as its doing vsprintf for one item in the array.

Expected: WHERE blah IN (:where1, :where2, :where3, :where4, :where5);
Result: WHERE blah IN (:where);

This results in an exception being thrown stating that you're passing more values than you've included in the statement.

https://github.com/ralphschindler/Zend_Db-Examples/issues/1
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 - Zend\Db and IN (1,2,3,4,5) only printing out IN (1)

ralphschindler
Hey intellix,

I'm aware of this issue, and it has been captured here:

http://framework.zend.com/issues/browse/ZF2-195

I should have a fix for this shortly, I will update you when it's fixed.

Thanks!
Ralph Schindler
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 - Zend\Db and IN (1,2,3,4,5) only printing out IN (1)

intellix
Hey, I'm not sure if this is caused by the same issue or not but I'm also running into a similar problem with 'IN' when doing query() and execute() for array IN parameters.

I had a little google and look around stackoverflow and I think it's a common problem anyway?

$idList = array(1,2);

$sql = "SELECT * FROM table WHERE id IN (" . $fp('idList') . ")";
$statement = $adapter->query($sql);
$rowSet = $statement->execute(array(
    'idList' => $idList
));

I tried a few bits and bobs but it seems the only way I can get this to work is to implode the values directly into the SQL which defeats the whole purpose of prepare, execute. Is there actually any way to send in array parameters for IN within MySQL etc anyway? I would have thought in MySQL that the following would use the same query plan anyway:

SELECT * FROM table WHERE id IN (1)
SELECT * FROM table WHERE id IN (1,2,3)
SELECT * FROM table WHERE id IN (1,2,3,4,5,6,7,8,9,10);

Thanks to you guys I've only just learnt about the benefits of prepare, execute as before ZF I was using mysql_query.... so I guess I don't know enough about what's going on yet :D
Loading...