2009-08-27 9 views
11

मुझे ड्रूपल व्यू में कुछ फ़िल्टरों के बीच एक या ऑपरेटर को लागू करने की आवश्यकता है। डिफ़ॉल्ट रूप से, ड्रूपल और प्रत्येक फ़िल्टर एक साथ।या ड्रूपल व्यू फ़िल्टर में ऑपरेटर

hook_views_query_alter(&$view, &$query) 

का उपयोग कर रहा क्वेरी (वर $ क्वेरी) का उपयोग कर सकते हैं, और मैं बदल सकते हैं या तो द्वारा:

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

'OR', या

$query->group_operator 

'OR'

समस्या है हालांकि, मुझे हर जगह या इसकी आवश्यकता नहीं है। मैंने दोनों को अलग-अलग या अलग-अलग बदलने की कोशिश की है, और यह वांछित परिणाम नहीं देता है।

ऐसा लगता है कि उन मानों को बदल रहा है, या हर जगह रखता है, जबकि मुझे => (फ़िल्टर 1 और फ़िल्टर 2) या (फ़िल्टर 3) की आवश्यकता होती है, इसलिए केवल 1 या।

मैं सिर्फ दृश्य की क्वेरी जांच कर सकता है, अगर यह ऐसा इसे संशोधित, और db_query के माध्यम से चलाने के लिए, लेकिन यह है कि सिर्फ गंदा है ..

कोई सुझाव?

अग्रिम में Thx।

+0

इस पर एक सेकंड के रूप में, दृश्य के लिए क्वेरी बदलने का भी उपयोग कर रहा है - मैं इस मार्ग को आजमाने की कोशिश कर रहा हूं। http://www.brianfending.com/content/better-wheres-your-drupal-forums-hookviewsqueryalter – cgp

+0

क्वेरी बदलता है जो @ वोडडे 'गंदे' के रूप में संदर्भित है, हम एक दृष्टिकोण के माध्यम से क्वेरी क्लॉज को संशोधित करने का प्रयास कर रहे हैं एपीआई कॉल –

+1

ड्रूपल और दृश्य संस्करण निर्दिष्ट करना अच्छा होगा। –

उत्तर

2

यदि आप इसे view_query_alter हुक के साथ करना चाहते हैं, तो आपको $ query-> add_where() का उपयोग करना चाहिए जहां आप यह निर्दिष्ट कर सकते हैं कि यह AND या OR है। विचारों से/शामिल/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) 
+1

मैंने इसे स्वयं करने की कोशिश की और इस विधि का उपयोग '(फ़िल्टर 1 या फ़िल्टर 2) और (फ़िल्टर 3 या फ़्लिटर 4)' में किया है, जो कि हम जो खोज रहे हैं उसका विरोध है: '(फ़िल्टर 1 और फ़िल्टर 2) या (फ़िल्टर 3 और फिल्टर 4) '। इसके लिए –

7

दुर्भाग्य से यह अभी भी Views2 में एक अनुपलब्ध विशेषता है। It has long been asked for और कुछ समय पहले वादा किया गया था, लेकिन यह काम का एक मुश्किल टुकड़ा और is now scheduled for Views3 लगता है।

इस बीच आप उस धागे में उल्लिखित Views Or मॉड्यूल का प्रयास कर सकते हैं। आज तक, यह अभी भी देव स्थिति में है, लेकिन सक्रिय रूप से बनाए रखा प्रतीत होता है और the issue queue खराब नहीं दिखता है, इसलिए आप इसे आज़मा सकते हैं।

+0

+1 कि यह Views2 में नहीं है।'$ this-> क्वेरी-> जहां [$ समूह] ['प्रकार'] = 'OR'; 'काम नहीं करता है, और' $ this-> क्वेरी-> group_operator = 'OR'; 'जहां सभी ऑपरेटरों को बीच में परिवर्तित किया जाता है या तो खंड। अधिक निराश। –

12

आप दृश्य का उपयोग कर रहे 3/ड्रुपल 7 और इस सवाल का जवाब की तलाश में हैं, तो यह सही दृश्य में शामिल किया हुआ है। जहां यह फ़िल्टर के बगल में "जोड़ें" कहता है, ड्रॉपडाउन पर क्लिक करें, फिर "और/या; पुनर्व्यवस्थित करें" पर क्लिक करें। यह वहां से स्पष्ट होना चाहिए।

+0

+1, यह वही करता है जो मुझे चाहिए था। एक बार वहां पहुंचने के बाद फील्ड समूह को भी देखें। यह एकमात्र हिस्सा था जिसने मुझे – Zach

+1

+1 एहसास करने के लिए कुछ मिनट लग गए शानदार दृश्य शानदार है! – bandolero

+0

मेरे पास व्यू 3 है, और मुझे नहीं लगता "और/या;" बस "पुनर्व्यवस्थित"? – Brittany

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++; 
    } 
    } 
} 
संबंधित मुद्दे