11

Drupalビューのいくつかのフィルター間にOR演算子を実装する必要があります。デフォルトでは、DrupalANDはすべてのフィルターを一緒に使用します。

を使用して

hook_views_query_alter(&$view, &$query)

クエリ(var $ query)にアクセスでき、次のいずれかを変更できます。

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

'OR'、または

$query->group_operator 

'OR'に

ただし、問題は、どこにでもORが必要ないことです。両方を別々にORに変更しようとしましたが、目的の結果が得られません。

これらの値を変更しているようで、ORはどこにでも配置されますが、必要なのは=>(フィルター1 ANDフィルター2)OR(フィルター3)なので、1つのORだけです。

ビューのクエリを確認し、コピーして変更し、db_queryを実行するだけで済みますが、それはただの汚いです。

助言がありますか ?

事前にThx。

4

4 に答える 4

12

Views 3 / Drupal 7を使用していて、この質問に対する答えを探している場合は、Viewsに直接組み込まれています。フィルタの横に「追加」と表示されている場合は、ドロップダウンをクリックしてから、[および/または;再配置]をクリックします。そこから明らかなはずです。

于 2011-12-15T04:04:14.613 に答える
7

残念ながら、これはまだViews2に欠けている機能です。それは長い間求められ、しばらく前に約束されていましたが、トリッキーな作業のようで、現在Views3で予定されています

それまでの間、そのスレッドで言及されているビューまたはモジュールを試すことができます。今日の時点ではまだ開発ステータスですが、アクティブに維持されているようで、問題キューは悪くないように見えるので、試してみることをお勧めします。

于 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 に答える