We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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?



34.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
]);

https://docs.phalcon.io/en/latest/reference/models.html#finding-records