Improved Nested Queries In Cdbcriteria


(Duynguyen0511) #1



$criteria=new CDbCriteria;		

$criteria->addCondition("active_to >= :active_to and 

job_id in(select distinct job_id from job_req_trade where trade_id 

    in (select trade_id from user_trade where registration_id=:registration_id))  ");

$criteria->params = array('registration_id'=>$reg_id, 'active_to' => date('Y-m-d'));

$dataProvider = new CActiveDataProvider('Job', array(

	'criteria'=>$criteria,			

));



The above returns jobs matching a job seeker’s trades skills. How can I improve the above condition in CDbCriteria. it has double “IN”.

Thanks


(Duynguyen0511) #2

any suggestions please ?


(Cedotoljic) #3

For sql IN you should use


$criteria->addInCondition($column,$values,$operator='AND')


$criteria->addInCondition('some_id', array(1,2,3)); // default operator is AND 




$criteria2->addInCondition('another_id', array(1,2,3), 'OR'); //default operator is AND you can change to OR



Merge $critera and $criteria2:




$criteria->mergeWith($criteria2);