m.dhouibi
(Mohamed Dhouibi1985)
August 15, 2014, 12:05pm
1
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
alirz23
(Alirz23)
August 15, 2014, 12:08pm
2
$criteria->compare('language.name', $this->language_search, true, 'AND');
$criteria->compare('country.name', $this->country_search, true);
Gregurco
(Gregurco Vlad)
August 15, 2014, 12:11pm
3
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’.
m.dhouibi
(Mohamed Dhouibi1985)
August 15, 2014, 12:15pm
4
alirz23:
$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],
Gregurco
(Gregurco Vlad)
August 15, 2014, 12:21pm
5
Write there your whole query generation code or give more information about sql error.
m.dhouibi
(Mohamed Dhouibi1985)
August 15, 2014, 1:08pm
6
$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’
m.dhouibi
(Mohamed Dhouibi1985)
August 15, 2014, 1:41pm
7
I added ‘AND’ to all criterias but still not working,
its always filtering everything dependently
m.dhouibi
(Mohamed Dhouibi1985)
August 15, 2014, 1:46pm
8
Solve it!
@alirz23 : I need to add ‘OR’ not ‘AND’
Thank you everyone.
m.dhouibi
(Mohamed Dhouibi1985)
August 15, 2014, 6:10pm
9
Please! I have a new question(problem)!
how i can manage the parentheses with OR and AND
in the criteria??
(… OR…OR) AND (…)
m.dhouibi
(Mohamed Dhouibi1985)
August 15, 2014, 6:19pm
10
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
georaldc
(Georaldc)
August 15, 2014, 8:41pm
11
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”’);
alirz23
(Alirz23)
August 17, 2014, 12:42pm
12
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);