欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How do I remove the “where” clause from a structured query object in hook_query_alter()?

Jumping off of this question, I'd like to know how to gracefully detect and remove each snippet in the WHERE clause of my query object in plain old hook_query_alter(), not hook_views_query_alter() (as the Views query object seems structured in some entirely different way (frustrating!). The documentation vaguely suggests that this is possible by showing how you can create a reference to your query conditions that you can then manipulate, by starting with this:
$where =& $query->conditions();

... but the object(s) within are utterly confusing, and I've been struggling to remove or replace existing snippets. I've explored /includes/select.inc and /includes/query.inc but the classes therein don't seem to provide any methods for fine-tune manipulation.

思路是 

$where =& $query->conditions(); // Remove the second condition.

unset($where[1]); //根据$where值 进行相应的unset
//也许是
unset($where[1]['arg'][1]) unset($where[1]['clauses'][1])



来自  http://drupal.stackexchange.com/questions/30685/how-do-i-remove-the-where-clause-from-a-structured-q...

How to remove "where" clause in views 3

I would like to remove the where clause for the city and postal_code filter field. I found the code below for D6 but it's not working.

<?php
function distprox_mini_views_query_alter(&$view, &$query) {
  if ($view->name == 'search' && $view->current_display == 'page_3') {
    // Remove the exposed filter conditions from the WHERE clause (WHERE city = MyCityInput)
    // and remove the corresponding args that were passed via the exposed filters
    // This effectively turns the city and zip code exposed filters into just text fields
    // that get used in distprox_mini_exposed_filter_proximity();

    $city = $view->exposed_raw_input['city'];
    $zip = $view->exposed_raw_input['postal_code'];

    // Remove the city or zip arguments (from the exposed filters)
    foreach ($query->where[0]['args'] as $key => $arg) {
      if ($arg == $city || $arg == $zip) {
        unset($query->where[0]['args'][$key]);
      }
    }

    // Remove the corresponding WHERE clause additions
    foreach ($query->where[0]['clauses'] as $key => $clause) {
      if (strpos($clause, 'postal_code') !== FALSE || strpos($clause, 'city') !== FALSE) {
        unset($query->where[0]['clauses'][$key]);
      }
    }
  }
}
?>

Comments

fehin’s picture

 

Status:Active» Closed (fixed)

It was a lot easier than I thought. I dsm the query and did it like below. Not sure it's the perfect way to do it but it's working.

<?php
    function distprox_mini_views_query_alter(&$view, &$query) {
    //dsm($query);
      if ($view->name == 'search' && $view->current_display == 'page_3') 
        unset($query->where[1]['conditions'][0]);
        unset($query->where[1]['conditions'][1]);
      }
    }
?>
jenlampton’s picture

 

Issue summary:View changes

I'm seeing Cannot access protected property SelectQuery::$where. Guessing things have changed a bit since 2012 :)

edit: looks like the where is now buried a bit deeper, something more like this...

$query->alterMetaData['view']->query->where[1]['conditions'][2]



 

来自   https://www.drupal.org/node/1567520

 

普通分类: