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 with IN operator

Hi Guys, can someone please give me a example of binding a array for a IN operator in find. eg.

$robots = Robots::find(
            [
                'conditions' => 'type_id IN :types:',
                'bind'       => [
                    "type_ids" => [1,2,3],
                ]
            ]
        );

The above gives SQL syntax errors.

I have tried this as well

$robots = Robots::find(
            [
                'conditions' => 'type_id IN (:types:)',
                'bind'       => [
                    "type_ids" => implode(",", [1,2,3]),
                ]
            ]
        );

but I still get errors

I found a old post https://forum.phalcon.io/discussion/3469/modelfindcol-in-with-bind Is there a better way in phalcon 3.4 or 4.0?



1.1k
Accepted
answer

Have you already tried this?

$robots = Robots::find(
            [
                'conditions' => 'type_id IN ({types_ids:array})',
                'bind'       => [
                    "type_ids" => [1,2,3]
                ]
            ]
        );

Awesome it works, thanks a lot.