function SearchQuery::countQuery

Same name in other branches
  1. 9 core/modules/search/src/SearchQuery.php \Drupal\search\SearchQuery::countQuery()
  2. 8.9.x core/modules/search/src/SearchQuery.php \Drupal\search\SearchQuery::countQuery()
  3. 10 core/modules/search/src/SearchQuery.php \Drupal\search\SearchQuery::countQuery()
  4. 11.x core/modules/search/src/SearchQuery.php \Drupal\search\SearchQuery::countQuery()

Builds the default count query for SearchQuery.

Since SearchQuery always uses GROUP BY, we can default to a subquery. We also add the same conditions as execute() because countQuery() is called first.

Overrides SelectQueryExtender::countQuery

File

modules/search/search.extender.inc, line 523

Class

SearchQuery
Do a query on the full-text search index for a word or words.

Code

public function countQuery() {
    // Clone the inner query.
    $inner = clone $this->query;
    // Add conditions to query.
    $inner->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type');
    $inner->condition($this->conditions);
    // Remove existing fields and expressions, they are not needed for a count
    // query.
    $fields =& $inner->getFields();
    $fields = array();
    $expressions =& $inner->getExpressions();
    $expressions = array();
    // Add the sid as the only field and count them as a subquery.
    $count = db_select($inner->fields('i', array(
        'sid',
    )), NULL, array(
        'target' => 'slave',
    ));
    // Add the COUNT() expression.
    $count->addExpression('COUNT(*)');
    return $count;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.