How to change condition on the fly


How could I change condition from AND to OR on the fly ?

for example:

		$d = User::model()->with(array(

			'myScloset' => array(

				'joinType' => 'LEFT JOIN',

				'condition' => 't1.whoAdd=82 OR'


			'myVcloset'  => array(

				'joinType' => 'LEFT JOIN',

				'condition' => 't2.whoAdd=82'



in this result i got next sql:

SELECT ... FROM `User` 

LEFT JOIN `MySketchCloset` t1 ON (t1.`uid`=`User`.`id`) 

LEFT JOIN `MyVideoCloset` t2 ON (t2.`uid`=`User`.`id`) 

WHERE (t1.whoAdd=82) 

AND (t2.whoAdd=82

But i need to get


WHERE (t1.whoAdd=82) 

OR (t2.whoAdd=82

How can i fix it ?


As I can see from ActiveRecord code this task can not be done with options. Merging of two related tables is done in CActiveRelation::mergeWith (in the framework\db\ar\CActiveRecord.php). And generated criterial are always joined with "AND".

In your case you can use some code like this:

$criteria=new CDbCriteria;

$criteria->join = 'LEFT JOIN `MySketchCloset` t1 ON (t1.`uid`=`User`.`id`) 

LEFT JOIN `MyVideoCloset` t2 ON (t2.`uid`=`User`.`id`)';

$criteria->condition = '(t1.whoAdd=82) AND (t2.whoAdd=82)';


Oh Thanks. it’s great idea! :slight_smile: