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

phalcon odm "IN (3,4,5)"

Is there anything like orm IN ARRAY() parameter in odm ?

$activity = UserActivityNo::find(array("conditions" => array("user_id IN (".implode(',',$user_ids).")")));
user_id IN (53121,15300,24872,985,112502,6)

I made that but I think it's false . Cuz it's not responding anything



2.0k

this way maybe:

$activity = UserActivityNo::find(array("conditions" => "user_id IN (".implode(',',$user_ids).")"));
edited Jun '14

I write IN statements like this:

Edit: this code not work properly

$activity = UserActivityNo::find(array(
    "conditions" => "user_id IN (:user_ids:)",
    "bind" => array(
        "user_ids" => join(',', $user_ids)
    )
));


2.0k

I think its not working cause the model escape the binded values, so ur query will be this: select * from useractivityno where user_id IN('user_id,user_id');

Or im in a mistake?

I test this method and I see that you're right. I must review my projects because I may use this method somewhere. Query is valid but not working properly.

edited Jun '14

I'm using this code:

    class MysqlAdapter extends \Phalcon\Db\Adapter\Pdo\Mysql
    {
        static public function inOperator($array)
        {
            $randomstring = substr("abcdefghijklmnopqrstuvwxyz", mt_rand(0,25), 1);

            $placeholders = array();
            $bindparams = array();
            foreach ($array as $key => $value)
            {
                //Numeric placeholder is not allowed, prepend a random char before the number
                if (is_int($key) || ctype_digit($key))
                    $key = sprintf("%s%d", $randomstring, $key);

                $placeholders[] = ":$key:";
                $bindparams[ $key ] = $value;
            }

            return array(
                'placeholders'      => implode(",", $placeholders),
                'bindparams'        => $bindparams,
            );
        }
    }

    //Then, in the controller or model:
    $inparams = MysqlAdapter::inOperator($user_ids);
    $activity = UserActivityNo::find(array(
        "conditions" => sprintf("user_id IN (%s)", $inparams['placeholders']),
        "bind" => $inparams['bindparams'],
    );