How do I bind an array to an IN condition with findAllBySql() ?

Hi,

Is there a way to bind an array to a placeholder parameter in a "IN" condition of a SQL query the same way as you bind other types?

For example, this works:


$value='something';

$sql="SELECT * FROM sometable WHERE someattr=:value";

$params=array(':value'=>$value);

$somemodel->findAllBySql($sql, $params);

However, this won’t work:


$values=array('foo', 'bar', 'boo');

$sql="SELECT * FROM sometable WHERE someattr IN (:values)";

$params=array(':values'=>$values);

$somemodel->findAllBySql($sql, $params);

How do I do that?

Or is it impossible at all to bind an array to a prepared statement?

I know I can do


 $sql="SELECT * FROM sometable WHERE someattr IN (".implode(',',$values).")";

and forget the $params, but I’m looking for the elegant way.

thanks

m.

Hi,

I dont think it is possible to work with $params as array by using findAllBySql method

but as alternative you can use addInCondition method by using CDbCriteria as demontred

in documentation example


$criteria->addInCondition('id',array(1,2,3,4,5,6)); 

In your case it will be




                $values=array('foo', 'bar', 'boo'); 

                $criteria=new CDbCriteria; 

		$criteria->addInCondition('someattr',$values);

		$example=YouModel::model()->findAll($criteria);



Hope it will be helpfull.