Tismon Varghese
Tismon Varghese

Reputation: 869

`AND` and `OR` condition together in `Yii` `ActiveRecord` `findAllByAtributes`

I have a controller function which prepare a condition depending upon some column names (created_user_id, status_id, company_id ). The record is fetched from a table jobs and if some condition is satisfied, we have to fetch some more rows with specific job_id. Example is giving below.

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

How can i write this condition with findAllByAttributes OR using findAll ? I tried with CbCriteria; but i have no idea how to combine this AND and OR condition together. Any help will be much appreciated.

Upvotes: 2

Views: 510

Answers (2)

Criesto
Criesto

Reputation: 1991

There are multiple ways:

$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);

Or,

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

Upvotes: 1

Insane Skull
Insane Skull

Reputation: 9358

Try using addInCondition

For Example,

$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);

Upvotes: 1

Related Questions