Searching Criteria

Hi there,

I wondering how I can search with multiple criterias independently from each other?

[b]An example:[/b]

Every file has(id, name…language, country).

If I enter in my search input fields:

-Language= English.

-Country = Spain.

$criteria->compare('', $this->language_search, true);

 $criteria->compare('', $this->country_search, true);     

===>The result will be :all the files from Spain with English language.

But what I need is: all the files from Spain AND all files with English language.

How this could be done?

Thank you

$criteria->compare('', $this->language_search, true, 'AND');

 $criteria->compare('', $this->country_search, true);

$operator - the operator used to concatenate the new condition with the existing one. Defaults to ‘AND’.

Hi Alirz23, thank you for your answers always,

, after i added the ‘AND’,

i had an error : CDbCommand failed to execute the SQL statement: SQLSTATE[42000],

Write there your whole query generation code or give more information about sql error.

$criteria = new CDbCriteria(array('condition' => ''));//MO

        $criteria->with = array('source', 'language', 'workflows', 'topics', 'country', 'allFileLocks', 'user','dep_id');//MO

        $criteria->group = '';

        $criteria->together = true;

        if (!empty($this->workflow_search))

            $criteria->compare('', $this->workflow_search->id, false);

        if (!empty($workflow_id))

            $criteria->compare('', $workflow_id, false);

        $criteria->compare('source_url', $this->source_url, true);




       $criteria->addInCondition('', $this->multiple_id_search,'AND');


        } if($this->country_search_form!=null){


       $criteria->compare('', $this->country_search_form,true,'AND');



I want to apply the ‘AND’ with ‘$this->multiple_id_search’ and’$this->country_search_form’

I added ‘AND’ to all criterias but still not working,

its always filtering everything dependently

Solve it!

@alirz23 : I need to add ‘OR’ not ‘AND’ :)

Thank you everyone.

Please! I have a new question(problem)!

how i can manage the parentheses with OR and AND

in the criteria??

(… OR…OR) AND (…)

I need ,( ( ) AND ((t.language=English) OR ( OR(…) )…

The and t.department ==>should be always as an condition.!

Please help, with Yii criterias :)


Unless there’s a better way, you might have to use the addCondition method and combine your criteria’s into one condition:

$criteria->compare(‘active’, 1);

$criteria->compare(‘department’, 1);

$criteria->addCondition(‘language = “English” OR country = “Spain”’);

Hope this helps

$criteria->compare('active', 1);

$criteria->compare('department', 1);

// you don't need to check if a value is empty

// if it is empty Yii will just ignore it

$criteria->compare('language', $language, true, 'OR');

$criteria->compare('country', $country, true);