我创建了自己的模块来侦听特定的视图名称和current_display。我正在尝试编写一个在以下WHERE语句中插入OR条件的函数。
WHERE (( (field_data_field_size_category.field_size_category_value LIKE '%11 - 20 fl oz%' ESCAPE '\\')
AND (field_data_field_neck_finish.field_neck_finish_value BETWEEN '21' AND '40') ))
理想的解决方案是再插入两个OR语句如下:
WHERE (( (field_data_field_size_category.field_size_category_value LIKE '%11 - 20 fl oz%' ESCAPE '\\')
AND (field_data_field_neck_finish.field_neck_finish_value BETWEEN '21' AND '40' OR
field_data_field_neck_finish_2.field_neck_finish_value_2 BETWEEN '21' AND '40' OR
field_data_field_neck_finish_3.field_neck_finish_value_3 BETWEEN '21' AND '40') ))
或者使用以下分组:
WHERE (( (field_data_field_size_category.field_size_category_value LIKE '%11 - 20 fl oz%' ESCAPE '\\')
AND (field_data_field_neck_finish.field_neck_finish_value BETWEEN '21' AND '40') OR
(field_data_field_neck_finish_2.field_neck_finish_value_2 BETWEEN '21' AND '40') OR
(field_data_field_neck_finish_3.field_neck_finish_value_3 BETWEEN '21' AND '40') ))
我试图改变查询的代码如下:
/**
* Implements hook_views_query_alter().
*/
function bottle_finder_query_views_query_alter(&$view, &$query) {
if ($view->name == 'product_listing_page' && $view->current_display == 'listing') {
foreach($query->where[1][conditions] as $key=>$val){
if($val['field']=="field_data_field_neck_finish.field_neck_finish_value"){
//dpm($query->where[1][conditions],$name=NULL,$type='status');
$query->add_where_expression(2,"field_data_field_neck_finish_2.field_neck_finish_value2",array('value'=>array('0'=>$val['value'][0],'1'=>$val['value'][1]),'operator'=>'BETWEEN'));
dpm($val['field'],$name=NULL,$type='status');
}
}
dpm($query->where[1][conditions],$name=NULL,$type='status');
drupal_set_message("I can make changes to the view here..");
}
}
提前感谢您提供任何指导。
答案 0 :(得分:0)
你有这样的想法吗?
function bottle_finder_query_views_query_alter(&$view, &$query) {
...
$filter = $query->createFilter('OR');
$filter->condition('some field machine name','some value');
$query->filter($filter);
这只是非常粗略地写了,让你知道它应该如何工作(保持手指交叉: - ))。