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

Binding IN condition

Hello.

I think that code says better.

$users            = Users::find(/* ... */);
// PHP 5.3 sucks
$usersIds         = array_map(function ($u) { return $u['id']; }, $users->toArray());
$implodedUsersIds = implode(',', $usersIds);

var_export($usersIds); // Good: array (0 => '5', 1 => '10', 2 => '14', 3 => '328', ...)

$cars1 = Cars::find(array(
    'conditions' => "userId IN ($implodedUsersIds)",
));
$cars2 = Cars::find(array(
    'conditions' => 'userId IN (:userId:)',
    'bind'       => array('userId' => $implodedUsersIds)
));

var_export($cars1->count()); // Good  : 40
var_export($cars2->count()); // WTF?! : 0

I've looked into database logging – these two SQLs:

SELECT `cars`.* FROM `cars` WHERE `cars`.`userId` IN (5, 10, 14, 328, ...) -- good
SELECT `cars`.* FROM `cars` WHERE `cars`.`userId` IN (:userId)             -- bad

What's wrong? Quer for getting users use 'conditions' and 'bind' too and it's ok.



6.3k
Accepted
answer

Phalcon can't do this now.

In PR https://github.com/phalcon/cphalcon/pull/2990 it implemented

edited Nov '15

Hi, you can use like this:

$phql = "SELECT * FROM Some\Robots WHERE id IN ({PARAM0:array})"; $robots = $modelsManager->executeQuery($phql, [ 'PARAM0' => [1, 2, 3] ]);

Update: it's supported in Phalcon 2.0.4: https://blog.phalcon.io/post/phalcon-2-0-4-released