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('language.name', $this->language_search, true);

 $criteria->compare('country.name', $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('language.name', $this->language_search, true, 'AND');

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

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#compare-detail

$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' => 't.active=1'));//MO

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

        $criteria->group = 't.id';

        $criteria->together = true;

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

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

        if (!empty($workflow_id))

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


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

.....

 if($this->multiple_id_search!=null){

            

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

      

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

            

       $criteria->compare('country.name', $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 ,( (t.active=1)AND(t.department=1) ) AND ((t.language=English) OR (t.country=Spain) OR(…) )…

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

Please help, with Yii criterias :)

Thanks

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);