问题描述
我的 GridView 中有几个下拉过滤器,在我的模型的搜索部分:
I have a couple of dropdown filters in my GridView, in the search part of my model I have:
if ($this->agent_primary != "") {
$criteria->addColumnCondition(array('agent_type_id'=>1, 'agent.agent_id'=>$this->agent_primary));
}
if ($this->agent_voice != "") {
$criteria->addColumnCondition(array('agent_type_id'=>2, 'agent.agent_id'=>$this->agent_voice));
}
if ($this->agent_commercial != "") {
$criteria->addColumnCondition(array('agent_type_id'=>3, 'agent.agent_id'=>$this->agent_commercial));
}
我需要以某种方式将其结合起来,因此如果有人选择了三个下拉列表中的两个(或全部三个),他们会得到正确的结果,目前 sql 在每个 addColumnCondition 的单独括号中都有:
I need to somehow combine this so if someone selects two of the three dropdowns (or all three) they get the correct results, currently the sql has there WHERE in separate brackets for each addColumnCondition:
WHERE (condition1a = a AND condition1b = b) AND (condition2a = a AND condition2b = b)
代替
WHERE (condition1a = a AND condition1b = b) OR (condition2a = a AND condition2b = b)
推荐答案
你可以简单地使用第三个参数 addColumnCondition
operator
/a>,设置为'OR'
:
You can simply use the third parameter operator
of addColumnCondition
, set it as 'OR'
:
if ($this->agent_primary != "") {
$criteria->addColumnCondition(array('agent_type_id'=>1, 'agent.agent_id'=>$this->agent_primary),'AND','OR');
}
你会得到:
WHERE (agent_type = 1 AND agent.agent_id = X) OR (condition2a = a AND condition2b = b)
这篇关于yii 在 CDbCriteria 中结合 addColumnCondition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!