`AND`和`OR`在`Yii``ActiveRecord``getAllByAtributes一起调节

时间:2015-11-03 04:38:38

标签: php yii1.x

我有一个控制器函数,它根据一些列名称(created_user_id,status_id,company_id)准备一个条件。记录是从表jobs获取的,如果满足某些条件,我们必须获取更多具有特定job_id的行。示例如下。

(created_user_id = 123 AND status_id = 15 AND company_id = 400 ) OR (job_id IN (18,22,34) )

如何使用findAllByAttributes或使用findAll来编写此条件?我试过CbCriteria;但我不知道如何将这个AND和OR条件结合在一起。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

尝试使用addInCondition

例如,

$criteria->condition(('created_user_id = 123 AND status_id = 15 AND company_id = 400', 'OR');
$criteria->addInCondition('job_id ', array('18','22','34'));
$relation_models = Relation::model()->findAll($criteria);

答案 1 :(得分:1)

有多种方式:

$criteria = new CDbCriteria;
$criteria->condition = '(created_user_id = 123 AND status_id = 15 AND company_id = 400) OR (job_id IN (18,22,34)))';
$models = TableName::model()->findAll($criteria);

或者,

$models = TableName::model()->findAll('column=:t4 OR something IN (2,4,6,7)', array(':t4'=>1));