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.

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.0k
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.