You are here

protected function Tables::ensureEntityTable in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Query/Sql/Tables.php \Drupal\Core\Entity\Query\Sql\Tables::ensureEntityTable()

Joins the entity table, if necessary, and returns the alias for it.

Parameters

string $index_prefix: The table array index prefix. For a base table this will be empty, for a target entity reference like 'field_tags.entity:taxonomy_term.name' this will be 'entity:taxonomy_term.target_id.'.

string $property: The field property/column.

string $type: The join type, can either be INNER or LEFT.

string $langcode: The langcode we use on the join.

string $base_table: The table to join to. It can be either the table name, its alias or the 'base_table' placeholder.

string $id_field: The name of the ID field/property for the current entity. For instance: tid, nid, etc.

array $entity_tables: Array of entity tables (data and base tables) where decide the entity property will be queried from. The first table containing the property will be used, so the order is important and the data table is always preferred.

Return value

string The alias of the joined table.

Throws

\Drupal\Core\Entity\Query\QueryException When an invalid property has been passed.

1 call to Tables::ensureEntityTable()
Tables::addField in core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
Adds a field to a database query.

File

core/lib/Drupal/Core/Entity/Query/Sql/Tables.php, line 353

Class

Tables
Adds tables and fields to the SQL entity query.

Namespace

Drupal\Core\Entity\Query\Sql

Code

protected function ensureEntityTable($index_prefix, $property, $type, $langcode, $base_table, $id_field, $entity_tables) {
  foreach ($entity_tables as $table => $mapping) {
    if (isset($mapping[$property])) {

      // Ensure a table joined multiple times through different index prefixes
      // has unique entityTables entries by concatenating the index prefix
      // and the base table alias. In this way i.e. if we join to the same
      // entity table several times for different entity reference fields,
      // each join gets a separate alias.
      $key = $index_prefix . ($base_table === 'base_table' ? $table : $base_table);
      if (!isset($this->entityTables[$key])) {
        $this->entityTables[$key] = $this
          ->addJoin($type, $table, "[%alias].[{$id_field}] = [{$base_table}].[{$id_field}]", $langcode);
      }
      return $this->entityTables[$key];
    }
  }
  throw new QueryException("'{$property}' not found");
}