So I created a function to make possible to pass arrays in bind.
class Model extends \Phalcon\Mvc\Model
{
public static function find($parameters = NULL)
{
return parent::find(self::_prepareBinds($parameters));
}
/*
* FROM:
* 0 => string 'id IN (:ids:)' (length=13)
* 'bind' =>
* array (size=1)
* 'ids' =>
* array (size=4)
* 0 => int 15
* 1 => int 17
* 2 => int 18
* 3 => int 19
* 'for_update' => boolean true
*
* TO:
* 0 => &string 'id IN (?0, ?1, ?2, ?3)' (length=22)
* 'bind' =>
* array (size=4)
* 0 => int 15
* 1 => int 17
* 2 => int 18
* 3 => int 19
* 'for_update' => boolean true
*/
private static function _prepareBinds($parameters = NULL)
{
// binding is allowed only in array
if (!is_array($parameters))
return $parameters;
// getting conditions from 0 or conditions parameter
if (!empty($parameters['conditions']))
$conditions =& $parameters['conditions'];
elseif (!empty($parameters[0]))
$conditions =& $parameters[0];
else
$conditions = '';
// finding largest already set placeholder to avoid conflicts
if (preg_match('/.*\?(\d+)/', $conditions, $matches))
$i = $matches[1] + 1;
else
$i = 0;
/*
* check if exists bind and replace all arrays to ?0 ?1 etc
*/
if (!empty($parameters['bind']))
foreach ($parameters['bind'] as $key => $binded)
if (is_array($binded))
{
$placeholders = array();
$binds = array();
foreach ($binded as $bind)
{
$placeholders[] = '?'.$i;
$parameters['bind'][$i] = $bind;
$i++;
}
unset($parameters['bind'][$key]);
$conditions = str_replace(':'.$key.':', implode(', ', $placeholders), $conditions);
}
return $parameters;
}
}
class Entity extends Model
{
}
class EntityController extends \Phalcon\Mvc\Controller
{
public function indexAction($checks)
{
$entityCollection = Entity::find(array(
'id IN (:ids:) AND entity_id IN (?99, :entities:)',
'bind' => array('ids' => array(15,17,18,19), 99 => 1, 'entities' => array(1,5,9,7)),
));
}
}
Result:
array (size=3)
0 => string 'id IN (:ids:) AND entity_id IN (?99, :entities:)' (length=48)
'bind' =>
array (size=3)
'ids' =>
array (size=4)
0 => int 15
1 => int 17
2 => int 18
3 => int 19
99 => int 1
'entities' =>
array (size=4)
0 => int 1
1 => int 5
2 => int 9
3 => int 7
'for_update' => boolean true
array (size=3)
0 => &string 'id IN (?100, ?101, ?102, ?103) AND entity_id IN (?99, ?104, ?105, ?106, ?107)' (length=77)
'bind' =>
array (size=9)
99 => int 1
100 => int 15
101 => int 17
102 => int 18
103 => int 19
104 => int 1
105 => int 5
106 => int 9
107 => int 7
'for_update' => boolean true