You are here

protected function SqlContentEntityStorage::buildQuery in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::buildQuery()
  2. 10 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::buildQuery()

Builds the query to load the entity.

This has full revision support. For entities requiring special queries, the class can be extended, and the default query can be constructed by calling parent::buildQuery(). This is usually necessary when the object being loaded needs to be augmented with additional data from another table, such as loading vocabulary machine name into terms, however it can also support $conditions on different tables.

Parameters

array|null $ids: An array of entity IDs, or NULL to load all entities.

array|bool $revision_ids: The IDs of the revisions to load, or FALSE if this query is asking for the default revisions. Defaults to FALSE.

Return value

\Drupal\Core\Database\Query\SelectInterface A SelectQuery object for loading the entity.

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php, line 692

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

protected function buildQuery($ids, $revision_ids = FALSE) {
  $query = $this->database
    ->select($this->baseTable, 'base');
  $query
    ->addTag($this->entityTypeId . '_load_multiple');
  if ($revision_ids) {
    $query
      ->join($this->revisionTable, 'revision', "[revision].[{$this->idKey}] = [base].[{$this->idKey}] AND [revision].[{$this->revisionKey}] IN (:revisionIds[])", [
      ':revisionIds[]' => $revision_ids,
    ]);
  }
  elseif ($this->revisionTable) {
    $query
      ->join($this->revisionTable, 'revision', "[revision].[{$this->revisionKey}] = [base].[{$this->revisionKey}]");
  }

  // Add fields from the {entity} table.
  $table_mapping = $this
    ->getTableMapping();
  $entity_fields = $table_mapping
    ->getAllColumns($this->baseTable);
  if ($this->revisionTable) {

    // Add all fields from the {entity_revision} table.
    $entity_revision_fields = $table_mapping
      ->getAllColumns($this->revisionTable);
    $entity_revision_fields = array_combine($entity_revision_fields, $entity_revision_fields);

    // The ID field is provided by entity, so remove it.
    unset($entity_revision_fields[$this->idKey]);

    // Remove all fields from the base table that are also fields by the same
    // name in the revision table.
    $entity_field_keys = array_flip($entity_fields);
    foreach ($entity_revision_fields as $name) {
      if (isset($entity_field_keys[$name])) {
        unset($entity_fields[$entity_field_keys[$name]]);
      }
    }
    $query
      ->fields('revision', $entity_revision_fields);

    // Compare revision ID of the base and revision table, if equal then this
    // is the default revision.
    $query
      ->addExpression('CASE [base].[' . $this->revisionKey . '] WHEN [revision].[' . $this->revisionKey . '] THEN 1 ELSE 0 END', 'isDefaultRevision');
  }
  $query
    ->fields('base', $entity_fields);
  if ($ids) {
    $query
      ->condition("base.{$this->idKey}", $ids, 'IN');
  }
  return $query;
}