Reputation: 4696
I am trying to pull information into a page using my model. The issue is that I need to use an IN
condition on my mysql using a variable.
Here is the code I use currently
$list_id = '1,3';
$clients = ListSubscriber::model()->findAll(array('condition'=>'list_id IN (:list_id)','params'=>array(':list_id'=>$list_id)));
I won't necessarily know how many numbers will be stored within $list_id
, hence the need for a variable to work with the IN
.
The code does execute without errors, but only seems to return the values for the first number of $list_id
, so in this case it only finds users where the list_id
= 1.
Any help is appreciated. I have found this question Yii addInCondition However they are using static values, which does not resolve my issue.
When I do use static values, the code executes with results as expected.
Upvotes: 1
Views: 425
Reputation: 12111
$list_ids = array(1,3);
$clients = ListSubscriber::model()->findAllByAttributes(array('list_id'=>$list_ids));
Upvotes: 2
Reputation: 8830
You can use addInCondition :
$list_id = '1,3';
$criteria = new CDbCriteria();
$arr_list_id = explode(",",$list_id);
$criteria->addInCondition("list_id ", $arr_list_id );
$clients = ListSubscriber::model()->findAll($criteria);
Upvotes: 3