Drupal 视图过滤器中的 OR 运算符 [英] OR operator in Drupal View Filters
问题描述
我需要在 Drupal 视图中的某些过滤器之间实现 OR 运算符.默认情况下,Drupal 将所有过滤器放在一起.
I need to implement an OR operator between some filters in a Drupal View. By default, Drupal AND's every filter together.
通过使用
hook_views_query_alter(&$view, &$query)
我可以访问查询( var $query ),我可以更改:
I can access the query ( var $query ) , and I can change either :
$query->where[0]['type']
到或",或
$query->group_operator
到或"
然而,问题是,我不需要到处都是 OR.我已经尝试将它们分别更改为 OR,但没有产生预期的结果.
The problem is however, that I do not need OR's everywhere. I've tried changing both of them to OR seperately, and it doesn't yield the desired result.
似乎改变了这些值,把 OR 放在任何地方,而我需要 =>(过滤器 1 和过滤器 2)OR(过滤器 3),所以只有 1 个 OR.
It seems changing those values, puts OR's everywhere, while I need => ( filter 1 AND filter 2 ) OR ( filter 3 ), so just 1 OR.
我可以只检查视图的查询,复制它,修改它,然后通过 db_query 运行它,但这只是很脏..
I could just check the Query of the View, copy it, modify it, and run it through db_query, but that's just dirty ..
有什么建议吗?
提前谢谢.
推荐答案
如果你想用 view_query_alter 钩子来做,你应该使用 $query->add_where() 来指定它是 AND 还是 OR.来自 views/include/query.inc
if you want do it with view_query_alter hook, you should use $query->add_where() where you can specify if it's AND or OR. From views/include/query.inc
/**
* Add a simple WHERE clause to the query. The caller is responsible for
* ensuring that all fields are fully qualified (TABLE.FIELD) and that
* the table already exists in the query.
*
* @param $group
* The WHERE group to add these to; groups are used to create AND/OR
* sections. Groups cannot be nested. Use 0 as the default group.
* If the group does not yet exist it will be created as an AND group.
* @param $clause
* The actual clause to add. When adding a where clause it is important
* that all tables are addressed by the alias provided by add_table or
* ensure_table and that all fields are addressed by their alias wehn
* possible. Please use %d and %s for arguments.
* @param ...
* A number of arguments as used in db_query(). May be many args or one
* array full of args.
*/
function add_where($group, $clause)
这篇关于Drupal 视图过滤器中的 OR 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!