class Tables implements TablesInterface (View source)

Adds tables and fields to the SQL entity query.

Properties

protected SelectInterface $sqlQuery
protected array $entityTables

Entity table array.

protected array $fieldTables

Field table array, key is table name, value is alias.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected EntityFieldManagerInterface $entityFieldManager

The entity field manager.

protected array $caseSensitiveFields

List of case sensitive fields.

Methods

__construct(SelectInterface $sql_query)

No description

string
addField(string $field, string $type, string $langcode)

Adds a field to a database query.

bool|null
isFieldCaseSensitive(string $field_name)

Determines whether the given field is case sensitive.

string
ensureEntityTable(string $index_prefix, string $property, string $type, string $langcode, string $base_table, string $id_field, array $entity_tables)

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

string
ensureFieldTable(string $index_prefix, FieldStorageDefinitionInterface $field, string $type, string $langcode, string $base_table, string $entity_id_field, string $field_id_field, string $delta)

Ensure the field table is joined if necessary.

string
addJoin(string $type, string $table, string $join_condition, string $langcode, string|null $delta = NULL)

Adds a join to a given table.

array|false
getTableMapping(string $table, string $entity_type_id)

Gets the schema for the given table.

string
addNextBaseTable(EntityType $entity_type, string $table, string $sql_column, FieldStorageDefinitionInterface $field_storage)

Add the next entity base table.

Details

__construct(SelectInterface $sql_query)

No description

Parameters

SelectInterface $sql_query

The SQL query.

string addField(string $field, string $type, string $langcode)

Adds a field to a database query.

Parameters

string $field

If it doesn't contain a dot, then an entity base field name. If it contains a dot, then either field name dot field column or field name dot delta dot field column. Delta can be a numeric value or a "%delta" for any value.

string $type

Join type, can either be INNER or LEFT.

string $langcode

The language code the field values are to be queried in.

Return Value

string

The return value is a string containing the alias of the table, a dot and the appropriate SQL column as passed in. This allows the direct use of this in a query for a condition or sort.

Exceptions

QueryException

bool|null isFieldCaseSensitive(string $field_name)

Determines whether the given field is case sensitive.

This information can only be provided after it was added with addField().

Parameters

string $field_name

The name of the field.

Return Value

bool|null

TRUE if the field is case sensitive, FALSE if not. Returns NULL when the field did not define if it is case sensitive or not.

protected string ensureEntityTable(string $index_prefix, string $property, string $type, string $langcode, string $base_table, string $id_field, array $entity_tables)

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.

Exceptions

QueryException

protected string ensureFieldTable(string $index_prefix, FieldStorageDefinitionInterface $field, string $type, string $langcode, string $base_table, string $entity_id_field, string $field_id_field, string $delta)

Ensure the field table is joined if necessary.

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.'.

FieldStorageDefinitionInterface $field

The field storage definition for the field being joined.

string $type

The join type.

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 $entity_id_field

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

string $field_id_field

The column representing the id for the field. For example, 'revision_id' or 'entity_id'.

string $delta

A delta which should be used as additional condition.

Return Value

string

The alias of the joined table.

protected string addJoin(string $type, string $table, string $join_condition, string $langcode, string|null $delta = NULL)

Adds a join to a given table.

Parameters

string $type

The join type.

string $table

The table to join to.

string $join_condition

The condition on which to join to.

string $langcode

The langcode we use on the join.

string|null $delta

(optional) A delta which should be used as additional condition.

Return Value

string

Returns the alias of the joined table.

protected array|false getTableMapping(string $table, string $entity_type_id)

Gets the schema for the given table.

Parameters

string $table

The table name.

string $entity_type_id

The entity type ID.

Return Value

array|false

An associative array of table field mapping for the given table, keyed by columns name and values are just incrementing integers. If the table mapping is not available, FALSE is returned.

protected string addNextBaseTable(EntityType $entity_type, string $table, string $sql_column, FieldStorageDefinitionInterface $field_storage)

Add the next entity base table.

For example, when building the SQL query for

Parameters

EntityType $entity_type

The entity type being joined, in the above example, User.

string $table

This is the table being joined, in the above example, {users}.

string $sql_column

This is the SQL column in the existing table being joined to.

FieldStorageDefinitionInterface $field_storage

The field storage definition for the field referencing this column.

Return Value

string

The alias of the next entity table joined in.