Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

PDOException when using "IN()" query

I'm creating an "IN" query like this:

$bind = array();
$bindTypes = array();
$i = 0;
$p = array();
foreach($values as $v){
    $p[] = '?' . $i;
    $bind[$i] = $v;
    $bindTypes[$i] = \Phalcon\Db\Column::BIND_PARAM_INT;
    $i++;
}

Now -- if I try to execute this using find():

$list = implode(',', $p);
ModelName::find(" MyValue in ($list) ", $bind, $bindTypes);

I get a PDOException: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':0, :1, :2) ORDER BY

However, it works just fine if I use the models manager:

$list = implode(',', $p);
$results = $this->modelsManager->executeQuery("SELECT * FROM ModelName WHERE MyValue in ($list) ", 
    $bind, $bindTypes);

As far as I can tell from my logs, the exact same sql is being generated behind the scenes for each method. Anyone have any ideas what's going on?



33.6k
Accepted
answer

Phalcon\Mvc\Model::find does not receive 3 parameters, the correct syntax is:

ModelName::find([
    "MyValue in ($list) ", 
    "bind" => $bind, 
    "bindTypes" => $bindTypes
]);

http://docs.phalconphp.com/en/latest/reference/models.html#finding-records