0

I process SelectQuery object using hook_query_TAG_after() or hook_query_alter().

I want to remove 'mytag' tag, which has been added as $query->addTag('mytag') somewhere else. Apparently, neither SelectQuery, nor QueryAlterableInterface has appropriate interface method.

Is there any means (besides SelectQuery subclassing) that I can use to remove a tag?

The reason may be the following (thanks Clive for asking):

I want to make a decision based on (expected) record set size inside hook_query_TAG_after()

I could do $countQuery = $query->countQuery(); $total = $countQuery->execute()->fetchField()

But, the cloned query will have same tag as original one and execute() will invoke the same hook ad infinitum.

1
  • Why do you need to remove it? Could you not just ignore it based on exactly the same condition that would have to be satisfied in order to remove it?
    – Clive
    Mar 20, 2012 at 11:20

2 Answers 2

1

I still can not find a way to remove a tag from query, but I found solution for my case of count query. The idea is to use another tag to stop endless recursion:

function mymodule_query_mytag_alter(QueryAlterableInterface $query) {

  if ($query->hasTag('counted')) {
    return;
  }

  // Prepare count query
  $countQuery = $query->countQuery();
  $countQuery->addTag('counted'); // Abort hook recursion

  $tables =& $countQuery->getTables(); // Query may have sub-queries

  foreach ($tables as &$table) { // Tag all subqueries
    if ($table['table'] instanceof SelectQueryInterface) {
      $table['table']->addTag('counted'); // Abort hook recursion
    }
  }

  $total = $countQuery->execute()->fetchField(); // execute() will initiate recursion!

 // Rest of processing

}
0

You should use something like this:

    $new_query = clone($db_query);
    unset($new_query->alterTags['search_api_db_facets_base']);
    if (empty($new_query->alterTags)) {
      unset($new_query->alterTags);
    }
    $search_api_current_entities = $new_query->execute()->fetchAll();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.