You are here

protected function EntityFieldQuery::fieldStorageQuery in RESTful 7.2

Copies field_sql_storage_field_storage_query() using left joins some times.

Overrides EntityFieldQuery::fieldStorageQuery

See also

field_sql_storage_field_storage_query()

1 call to EntityFieldQuery::fieldStorageQuery()
EntityFieldQuery::buildQuery in src/Util/EntityFieldQuery.php
Builds the SelectQuery and executes finishQuery().
1 method overrides EntityFieldQuery::fieldStorageQuery()
EntityFieldQuery::fieldStorageQuery in src/Util/EntityFieldQuery.php
Copies field_sql_storage_field_storage_query() using left joins some times.

File

src/Util/EntityFieldQuery.php, line 298
Contains \Drupal\restful\Util\EntityFieldQuery.

Class

EntityFieldQuery

Namespace

Drupal\restful\Util

Code

protected function fieldStorageQuery(SelectQuery $select_query) {
  if ($this->age == FIELD_LOAD_CURRENT) {
    $tablename_function = '_field_sql_storage_tablename';
    $id_key = 'entity_id';
  }
  else {
    $tablename_function = '_field_sql_storage_revision_tablename';
    $id_key = 'revision_id';
  }
  $table_aliases = array();
  $query_tables = NULL;
  $base_table = $this->metaData['base_table'];

  // Add tables for the fields used.
  $field_base_table = NULL;
  foreach ($this->fields as $key => $field) {
    $tablename = $tablename_function($field);
    $table_alias = _field_sql_storage_tablealias($tablename, $key, $this);
    $table_aliases[$key] = $table_alias;
    $select_query
      ->addMetaData('base_table', $base_table);
    $entity_id_key = $this->metaData['entity_id_key'];
    if ($field_base_table) {
      if (!isset($query_tables[$table_alias])) {
        $this
          ->addFieldJoin($select_query, $field['field_name'], $tablename, $table_alias, "{$table_alias}.entity_type = {$field_base_table}.entity_type AND {$table_alias}.{$id_key} = {$field_base_table}.{$id_key}");
      }
    }
    else {

      // By executing prePropertyQuery() we made sure that the base table is
      // the entity table.
      $this
        ->addFieldJoin($select_query, $field['field_name'], $tablename, $table_alias, "{$base_table}.{$entity_id_key} = {$table_alias}.{$id_key}");

      // Store a reference to the list of joined tables.
      $query_tables =& $select_query
        ->getTables();

      // Allow queries internal to the Field API to opt out of the access
      // check, for situations where the query's results should not depend on
      // the access grants for the current user.
      if (!isset($this->tags['DANGEROUS_ACCESS_CHECK_OPT_OUT'])) {
        $select_query
          ->addTag('entity_field_access');
      }
      if (!$this
        ->containsLeftJoinOperator($this->fields[$key]['field_name'])) {
        $field_base_table = $table_alias;
      }
    }
    if ($field['cardinality'] != 1 || $field['translatable']) {
      $select_query
        ->distinct();
    }
  }

  // Add field conditions. We need a fresh grouping cache.
  drupal_static_reset('_field_sql_storage_query_field_conditions');
  _field_sql_storage_query_field_conditions($this, $select_query, $this->fieldConditions, $table_aliases, '_field_sql_storage_columnname');

  // Add field meta conditions.
  _field_sql_storage_query_field_conditions($this, $select_query, $this->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');

  // If there was no field condition that created an INNER JOIN, that means
  // that additional JOINs need to carry the OR condition. For the base table
  // we'll use the table for the first field.
  $needs_or = FALSE;
  if (!isset($field_base_table)) {
    $needs_or = TRUE;

    // Get the table name for the first field.
    $field_table_name = key($this->fields[0]['storage']['details']['sql'][$this->age]);
    $field_base_table = _field_sql_storage_tablealias($field_table_name, 0, $this);
  }
  if (isset($this->deleted)) {
    $delete_condition = array(
      'value' => (int) $this->deleted,
      'operator' => '=',
      'or' => $needs_or,
    );
    $this
      ->addCondition($select_query, "{$field_base_table}.deleted", $delete_condition);
  }
  foreach ($this->entityConditions as $key => $condition) {
    $condition['or'] = $needs_or;
    $this
      ->addCondition($select_query, "{$field_base_table}.{$key}", $condition);
  }

  // Order the query.
  foreach ($this->order as $order) {
    if ($order['type'] == 'entity') {
      $key = $order['specifier'];
      $select_query
        ->orderBy("{$field_base_table}.{$key}", $order['direction']);
    }
    elseif ($order['type'] == 'field') {
      $specifier = $order['specifier'];
      $field = $specifier['field'];
      $table_alias = $table_aliases[$specifier['index']];
      $sql_field = "{$table_alias}." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
      $select_query
        ->orderBy($sql_field, $order['direction']);
    }
    elseif ($order['type'] == 'property') {
      $select_query
        ->orderBy("{$base_table}." . $order['specifier'], $order['direction']);
    }
  }
  return array(
    $select_query,
    $id_key,
  );
}