OR操作符在Drupal查看过滤器 [英] OR operator in Drupal View Filters

查看:98
本文介绍了OR操作符在Drupal查看过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Drupal视图中的某些过滤器之间实现一个OR运算符。
默认情况下,Drupal AND的每个过滤器一起。



通过使用

  hook_views_query_alter(& $ view,& $ query)

我可以访问查询(var $ query),我可以更改:

  $ query->其中[0] ['type'] 

到'OR'或

  $ query-> group_operator 

to'OR' p>

然而,问题是我无处不在。我已经尝试将它们两者都更改为OR,并且不会产生所需的结果。



似乎改变这些值,把OR放在任何地方,而我需要=>(过滤器1和过滤器2)或(过滤器3),所以只是1 OR。



我可以检查视图的查询,复制,修改它,并运行它通过db_query,但这只是脏..



任何建议?



提前Thx。

解决方案

view_query_alter钩子,你应该使用$ query-> add_where(),在这里你可以指定它是AND还是OR。从view / include / query.inc

  / ** 
*向查询添加一个简单的WHERE子句。调用者负责
*,确保所有字段都完全限定(TABLE.FIELD),而
*表中已经存在该查询。
*
* @param $ group
*将这些添加到的WHERE组;组用于创建AND / OR
*部分。组不能嵌套。使用0作为默认组。
*如果组不存在,将创建为AND组。
* @param $ clause
*要添加的实际子句。当添加where子句时,重要的是
*,所有表都由add_table或
* ensure_table提供的别名来处理,所有字段都由其别名wehn
*可能解决。请使用%d和%s作参数。
* @param ...
* db_query()中使用的一些参数。可能有很多args或一个
*数组的args。
* /
函数add_where($ group,$ clause)


I need to implement an OR operator between some filters in a Drupal View. By default, Drupal AND's every filter together.

By using

hook_views_query_alter(&$view, &$query)

I can access the query ( var $query ) , and I can change either :

$query->where[0]['type'] 

to 'OR', or

$query->group_operator 

to '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.

It seems changing those values, puts OR's everywhere, while I need => ( filter 1 AND filter 2 ) OR ( filter 3 ), so just 1 OR.

I could just check the Query of the View, copy it, modify it, and run it through db_query, but that's just dirty ..

Any suggestions ?

Thx in advance.

解决方案

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)

这篇关于OR操作符在Drupal查看过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆