您可以使用findAllByAttributes
:
$relation_models = Relation::model()->findAllByAttributes(array(
'group_id'=>array('2','3'),
'subject_id'=>array('4','5','7')
)
);
这会给你类似的东西(实际查询可能包含别名):
select * from relation where group_id IN (1,3) and subject_id IN (1,4,6,7)
但是,如果您想要:(where group_id IN (1,3) OR subject_id IN (1,4,6,7)
即 OR 而不是 AND),那么您将不得不使用CDbCriteria
:
$criteria = new CDbCriteria;
$criteria->condition = 'group_id IN (1,3) OR subject_id IN (1,4,6,7)'
$relation_models = Relation::model()->findAll($criteria);
你也可以使用addInCondition
:
$criteria->addInCondition('group_id', array('1','2'), 'OR');
$criteria->addInCondition('subject_id', array('1','4','6','7'), 'OR');
$relation_models = Relation::model()->findAll($criteria);
不过,请记住绑定参数,以防您从用户那里获取输入。像这样的东西:
$criteria->condition = 'group_id IN (:gid1, :gid2) OR subject_id IN (:sid1, :sid2, :sid3, :sid4)';
$criteria->params = array(':gid1'=>$xyz,':gid2'=>'2',':sid1'=>$uvw,':sid2'=>$abc,':sid3'=>'6',':sid4'=>'7');
$relation_models = Relation::model()->findAll($criteria);