protected function Query::prepare

Prepares the basic query with proper metadata/tags and base fields.

Return value

\Drupal\field_sql_storage\Entity\Query Returns the called object.

Throws

\Drupal\Core\Entity\Query\QueryException Thrown if the base table does not exists.

1 call to Query::prepare()
QueryAggregate::prepare in drupal/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/QueryAggregate.php
Overrides \Drupal\field_sql_storage\Entity::prepare().
1 method overrides Query::prepare()
QueryAggregate::prepare in drupal/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/QueryAggregate.php
Overrides \Drupal\field_sql_storage\Entity::prepare().

File

drupal/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Query.php, line 117
Definition of Drupal\field_sql_storage\Entity\Query.

Class

Query
The SQL storage entity query class.

Namespace

Drupal\field_sql_storage\Entity

Code

protected function prepare() {
  $entity_type = $this->entityType;
  $this->entityInfo = $this->entityManager
    ->getDefinition($entity_type);
  if (!isset($this->entityInfo['base_table'])) {
    throw new QueryException("No base table, invalid query.");
  }
  $base_table = $this->entityInfo['base_table'];
  $simple_query = TRUE;
  if (isset($this->entityInfo['data_table'])) {
    $simple_query = FALSE;
  }
  $this->sqlQuery = $this->connection
    ->select($base_table, 'base_table', array(
    'conjunction' => $this->conjunction,
  ));
  $this->sqlQuery
    ->addMetaData('entity_type', $entity_type);
  $id_field = $this->entityInfo['entity_keys']['id'];

  // Add the key field for fetchAllKeyed().
  if (empty($this->entityInfo['entity_keys']['revision'])) {

    // When there is no revision support, the key field is the entity key.
    $this->sqlFields["base_table.{$id_field}"] = array(
      'base_table',
      $id_field,
    );

    // Now add the value column for fetchAllKeyed(). This is always the
    // entity id.
    $this->sqlFields["base_table.{$id_field}" . '_1'] = array(
      'base_table',
      $id_field,
    );
  }
  else {

    // When there is revision support, the key field is the revision key.
    $revision_field = $this->entityInfo['entity_keys']['revision'];
    $this->sqlFields["base_table.{$revision_field}"] = array(
      'base_table',
      $revision_field,
    );

    // Now add the value column for fetchAllKeyed(). This is always the
    // entity id.
    $this->sqlFields["base_table.{$id_field}"] = array(
      'base_table',
      $id_field,
    );
  }
  if ($this->accessCheck) {
    $this->sqlQuery
      ->addTag($entity_type . '_access');
  }
  $this->sqlQuery
    ->addTag('entity_query');
  $this->sqlQuery
    ->addTag('entity_query_' . $this->entityType);

  // Add further tags added.
  if (isset($this->alterTags)) {
    foreach ($this->alterTags as $tag => $value) {
      $this->sqlQuery
        ->addTag($tag);
    }
  }

  // Add further metadata added.
  if (isset($this->alterMetaData)) {
    foreach ($this->alterMetaData as $key => $value) {
      $this->sqlQuery
        ->addMetaData($key, $value);
    }
  }

  // This now contains first the table containing entity properties and
  // last the entity base table. They might be the same.
  $this->sqlQuery
    ->addMetaData('age', $this->age);
  $this->sqlQuery
    ->addMetaData('simple_query', $simple_query);
  return $this;
}