Vod*_*dde 11 drupal drupal-views
我需要在Drupal View中的某些过滤器之间实现OR运算符.默认情况下,Drupal AND的每个过滤器都在一起.
通过使用
hook_views_query_alter(&$view, &$query)
Run Code Online (Sandbox Code Playgroud)
我可以访问查询(var $ query),我可以更改:
$query->where[0]['type']
Run Code Online (Sandbox Code Playgroud)
'或',或
$query->group_operator
Run Code Online (Sandbox Code Playgroud)
'OR'
但问题是,我到处都不需要OR.我已经尝试将它们分别更改为OR,并且它不会产生所需的结果.
它似乎改变了这些值,把OR放在任何地方,而我需要=>(滤波器1和滤波器2)或(滤波器3),所以只需1或.
我可以检查View的查询,复制它,修改它,然后通过db_query运行它,但那只是脏的..
有什么建议 ?
Thx提前.
dav*_*ber 12
如果你正在使用Views 3/Drupal 7并寻找这个问题的答案,它会直接进入Views.如果在过滤器旁边显示"添加",请点击下拉列表,然后点击"和/或重新排列".从那里应该是显而易见的.
如果你想用 view_query_alter 钩子来做,你应该使用 $query->add_where() ,你可以指定它是 AND 还是 OR。来自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)
Run Code Online (Sandbox Code Playgroud)