merge with criteria using OR

Hi Yii peeps,

Ok I have a query like so:


    // want to get only users with emails

    'joinType'=>'INNER JOIN',

    'condition'=>"address like :q",



the nameLike() function is a scope and does a $this->getCDbCriteria()->mergeWith() and adds 1 condition that checks the first_name and last_name or company name is like the search term (which is the $q parameter) I want to add an or address (email address) is like the search parameter. At the moment the condition is appended to the query with an AND.

I have checked on the forums and only found people solve this by adding the or statement to the same condition string. This does not work in my case as i get something like

And (or address like var) 

Any help would be great :slight_smile:

According to the API reference, the second paramete in the call to mergeWith() can control this.


Thanks tri,

After a bit of fumbling I managed to get it to work.

$c = Contact::model()->nameLike($q)->getDbCriteria()->mergeWith(array(

    'with'=>array('emails'=>array('joinType'=>'INNER JOIN')),

    'condition'=>'address like :q',


), false);

then I can call, Contact::model()->findAll() and it all works good.

Thanks for your help.

Ok Stumped again. When I add a limit: Contact::model()->findAll(array(‘limit’=>20)) the join condition is removed and I get a can’t find the column SQL error!?

Any one know why?