Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. SearchQuery.php

function 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 SelectExtender::countQuery

File

core/modules/search/src/SearchQuery.php, line 604

Class

SearchQuery
Search query extender and helper functions.

Namespace

Drupal\search

Code

public function countQuery() {
    if (!$this->executedPrepare) {
        $this->prepareAndNormalize();
    }
    // 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]');
    if (count($this->conditions)) {
        $inner->condition($this->conditions);
    }
    // Remove existing fields and expressions, they are not needed for a count
    // query.
    $fields =& $inner->getFields();
    $fields = [];
    $expressions =& $inner->getExpressions();
    $expressions = [];
    // Add sid as the only field and count them as a subquery.
    $count = $this->connection
        ->select($inner->fields('i', [
        'sid',
    ]), NULL);
    // Add the COUNT() expression.
    $count->addExpression('COUNT(*)');
    return $count;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal