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

Model::find() doesn't work with IN condition and bind params

This will not work:

$ids = [1,2,3];
Model::find(array(
    "conditions" => "id IN (?0)",
    "bind"       => array(0 => $ids)
));

Neither this:

$ids = implode("','", [1,2,3]);
Model::find(array(
    "conditions" => "id IN (?0)",
    "bind"       => array(0 => $ids)
));

The only thing that works is:

Model::find(array(
    "conditions" => "id IN (?0, ?1, ?2)",
    "bind"       => array(0 => 1, 1 => 1, 2 => 2)
));

But that sucks, because we have to build the placeholders ourselves. We should be able to pass an array to the bind params and the framework should know to transform it to the correct form. Most other frameworks do.

Nonono. It works perfectly for me, but with this solution you can try :

<?php

return self::query()
    ->inWhere('id', $arrayIds)
    ->getQuery()
    ->execute();

So indeed, you cannot use Model::find() and IN(), but you have a nice workaround :)



8.6k

Well, yes, there are always workarounds, but I was just pointing out this behaviour as a possible bug :)