Yii Framework Forum: Searching Criteria - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Searching Criteria multi filtring Rate Topic: -----

#1 User is offline   m.dhouibi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 11-July 14

Posted 15 August 2014 - 07:05 AM

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
0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,079
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 15 August 2014 - 07:08 AM

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

0

#3 User is offline   Gregurco 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 12-August 14

Posted 15 August 2014 - 07:11 AM

http://www.yiiframew...#compare-detail
$operator - the operator used to concatenate the new condition with the existing one. Defaults to 'AND'.
0

#4 User is offline   m.dhouibi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 11-July 14

Posted 15 August 2014 - 07:15 AM

View Postalirz23, on 15 August 2014 - 07:08 AM, said:

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


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],
0

#5 User is offline   Gregurco 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 12-August 14

Posted 15 August 2014 - 07:21 AM

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

#6 User is offline   m.dhouibi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 11-July 14

Posted 15 August 2014 - 08:08 AM

View PostGregurco, on 15 August 2014 - 07:21 AM, said:

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'
0

#7 User is offline   m.dhouibi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 11-July 14

Posted 15 August 2014 - 08:41 AM

I added 'AND' to all criterias but still not working,
its always filtering everything dependently
0

#8 User is offline   m.dhouibi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 11-July 14

Posted 15 August 2014 - 08:46 AM

Solve it!
@alirz23 : I need to add 'OR' not 'AND' :)
Thank you everyone.
0

#9 User is offline   m.dhouibi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 11-July 14

Posted 15 August 2014 - 01:10 PM

Please! I have a new question(problem)!
how i can manage the parentheses with OR and AND
in the criteria??
(... OR...OR) AND (...)
0

#10 User is offline   m.dhouibi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 11-July 14

Posted 15 August 2014 - 01:19 PM

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
0

#11 User is online   georaldc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 103
  • Joined: 08-July 11

Posted 15 August 2014 - 03:41 PM

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"');
0

#12 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,079
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 17 August 2014 - 07:42 AM

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

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users