11

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

通过使用

hook_views_query_alter(&$view, &$query)

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

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

到“或”,或

$query->group_operator 

到“或”

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

似乎改变了这些值,到处都是 OR,而我需要 =>(过滤器 1 和过滤器 2)或(过滤器 3),所以只有 1 个或。

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

有什么建议么 ?

提前谢谢。

4

4 回答 4

12

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

于 2011-12-15T04:04:14.613 回答
7

不幸的是,这仍然是 Views2 中缺少的功能。很久以前就有人要求并承诺过,但似乎是一项棘手的工作,现在计划用于 Views3

同时,您可以尝试该线程中提到的Views 或模块。截至今天,它仍处于开发状态,但似乎正在积极维护,问题队列看起来也不错,所以您可能想尝试一下。

于 2009-08-27T23:39:12.667 回答
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)
于 2009-08-29T11:19:51.870 回答
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++;
    }
  }
}
于 2012-06-29T10:14:42.870 回答