[Solved] Cdbcriteria : Mixing And / Or

Hi ,

I’m trying to build a request via CDbCriteria, mixing AND & OR :




$q = new CDbCriteria();

$q->condition = 't.created_by=:id';

$q->params = array(':id'=>Yii::app()->user->id);

$q->addSearchCondition('emails.metadata', $search, true, 'AND');

$q->addSearchCondition('emails.desc', $search, true, 'OR');

$q->addSearchCondition('mails.email', $search, true, 'OR');

$q->addSearchCondition('file', $search, true, 'OR');

Which build something like :


SELECT * FROM ... WHERE t.created_by = :id AND ... OR ... OR ... 

(no brackets)



My problem is that I want a request lile :


SELECT * FROM ... WHERE t.created_by = :id AND ( ... OR ... OR ... )

(with brackets grouping the OR)



How can I group my conditions ?

Thanks by advance.

Ok, I finally get it (searching for hours… and finding the answer just after posting the question here… Forum’s Murphy Law)

I have to use 2 CDbCriteria and to merge them :


$q = new CDbCriteria();

$q->addSearchCondition('emails.metadata', $search, true, 'AND');

$q->addSearchCondition('emails.desc', $search, true, 'OR');

$q->addSearchCondition('mails.email', $search, true, 'OR');

$q->addSearchCondition('file', $search, true, 'OR');




$u->condition = 't.created_by=:id';

$u->params = array(':id'=>Yii::app()->user->id);


$u->mergeWith($q);


$uArray  = u::model()->with('emails')->findAll( $u );



thanks…