function Tables::ensureEntityTable

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Query/Sql/Tables.php \Drupal\Core\Entity\Query\Sql\Tables::ensureEntityTable()
  2. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/Tables.php \Drupal\Core\Entity\Query\Sql\Tables::ensureEntityTable()
  3. 10 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 that is used 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 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 358

Class

Tables
Adds database 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. This way, if the same entity table is
            // joined several times for different entity reference fields, each join
            // gets a unique 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");
}

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