Drupal View过滤器中的OR运算符

时间:2009-08-27 11:20:37

标签: drupal drupal-views

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

使用

hook_views_query_alter(&$view, &$query)

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

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

到'OR',或

$query->group_operator 

到'OR'

但问题是,我到处都不需要OR。我已经尝试将它们分别更改为OR,并且它不会产生预期的结果。

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

我可以查看View的查询,复制它,修改它,然后通过db_query运行它,但那只是很脏..

有什么建议吗?

提前谢谢。

4 个答案:

答案 0 :(得分:12)

如果你正在使用Views 3 / Drupal 7并且正在寻找这个问题的答案,那么它会直接进入Views。在过滤器旁边显示“添加”的位置,单击下拉列表,然后单击“和/或;重新排列”。从那里就应该很明显了。

答案 1 :(得分:7)

不幸的是,这仍然是Views2中缺少的功能。 It has long been asked for并且在很久以前得到了承诺,但似乎是一项棘手的工作is now scheduled for Views3

在此期间,您可以尝试该线程中提到的Views Or模块。截至今天,它仍处于开发状态,但似乎得到了积极维护,the issue queue看起来并不坏,所以你可能想尝试一下。

答案 2 :(得分:2)

如果你想用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)

答案 3 :(得分:1)

我通过连接字符串添加它。

它与实现相对具体 - 人们需要使用字段来匹配以下代码中的OR - node.title和字段以匹配 - node_revisions.body在这种情况下。

确保node_revisions.body在查询中的额外代码。

/**
 * Implementation of hook_views_api().
 */
function eventsor_views_api() { // your module name into hook_views_api
  return array(
  'api' => 2,
  // might not need the line below, but in any case, the last arg is the name of your module
  'path' => drupal_get_path('module', 'eventsor'),
 );
}

/**
 *
 * @param string $form
 * @param type $form_state
 * @param type $form_id 
 */
function eventsor_views_query_alter(&$view, &$query) {

  switch ($view->name) {
    case 'Events':
      _eventsor_composite_filter($query);
      break;
  }
}

/**
 * Add to the where clause.
 * @param type $query 
 */
function _eventsor_composite_filter(&$query) {
  // If we see "UPPER(node.title) LIKE UPPER('%%%s%%')" - then add and to it.
  if (isset($query->where)) {

    $where_count = 0;
    foreach ($query->where as $where) {
      $clause_count = 0;
      if (isset($where['clauses'])) {
        foreach ($where['clauses'] as $clause) {
          $search_where_clause = "UPPER(node.title) LIKE UPPER('%%%s%%')";
          // node_data_field_long_description.field_long_description_value
          $desirable_where_clause = "UPPER(CONCAT_WS(' ', node.title, node_revisions.body)) LIKE UPPER('%%%s%%')";
          if ($clause == $search_where_clause) {
            //  $query->add_where('or', 'revisions.body = %s'); - outside of what we are looking for
            $query->where[$where_count]['clauses'][$clause_count] = $desirable_where_clause;

            // Add the field to the view, just in case.
            if (!isset($query->fields['node_revisions_body'])) {
              $query->fields['node_revisions_body'] = array(
                'field' => 'body',
                'table' => 'node_revisions',
                'alias' => 'node_revisions_body'
              );
            }
          }
          $clause_count++;
        }
      }
      $where_count++;
    }
  }
}