You are here

protected function EntityFieldQuery::prePropertyQuery in RESTful 7.2

Copy of propertyQuery() without the finishQuery execution.

Overrides EntityFieldQuery::prePropertyQuery

See also

\EntityFieldQuery::propertyQuery()

1 call to EntityFieldQuery::prePropertyQuery()
EntityFieldQuery::buildQuery in src/Util/EntityFieldQuery.php
Builds the SelectQuery and executes finishQuery().
1 method overrides EntityFieldQuery::prePropertyQuery()
EntityFieldQuery::prePropertyQuery in src/Util/EntityFieldQuery.php
Copy of propertyQuery() without the finishQuery execution.

File

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

Class

EntityFieldQuery

Namespace

Drupal\restful\Util

Code

protected function prePropertyQuery() {
  if (empty($this->entityConditions['entity_type'])) {
    throw new \EntityFieldQueryException(t('For this query an entity type must be specified.'));
  }
  $entity_type = $this->entityConditions['entity_type']['value'];
  $entity_info = entity_get_info($entity_type);
  if (empty($entity_info['base table'])) {
    throw new \EntityFieldQueryException(t('Entity %entity has no base table.', array(
      '%entity' => $entity_type,
    )));
  }
  $base_table = $entity_info['base table'];
  $base_table_schema = drupal_get_schema($base_table);
  $select_query = db_select($base_table);
  $select_query
    ->addExpression(':entity_type', 'entity_type', array(
    ':entity_type' => $entity_type,
  ));

  // Process the property conditions.
  foreach ($this->propertyConditions as $property_condition) {
    $this
      ->addCondition($select_query, $base_table . '.' . $property_condition['column'], $property_condition);
  }

  // Process the four possible entity condition.
  // The id field is always present in entity keys.
  $sql_field = $entity_info['entity keys']['id'];
  $this
    ->addMetaData('base_table', $base_table);
  $this
    ->addMetaData('entity_id_key', $sql_field);
  $id_map['entity_id'] = $sql_field;
  $select_query
    ->addField($base_table, $sql_field, 'entity_id');
  if (isset($this->entityConditions['entity_id'])) {
    $this
      ->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['entity_id']);
  }

  // If there is a revision key defined, use it.
  if (!empty($entity_info['entity keys']['revision'])) {
    $sql_field = $entity_info['entity keys']['revision'];
    $select_query
      ->addField($base_table, $sql_field, 'revision_id');
    if (isset($this->entityConditions['revision_id'])) {
      $this
        ->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['revision_id']);
    }
  }
  else {
    $sql_field = 'revision_id';
    $select_query
      ->addExpression('NULL', 'revision_id');
  }
  $id_map['revision_id'] = $sql_field;

  // Handle bundles.
  if (!empty($entity_info['entity keys']['bundle'])) {
    $sql_field = $entity_info['entity keys']['bundle'];
    $having = FALSE;
    if (!empty($base_table_schema['fields'][$sql_field])) {
      $select_query
        ->addField($base_table, $sql_field, 'bundle');
    }
  }
  else {
    $sql_field = 'bundle';
    $select_query
      ->addExpression(':bundle', 'bundle', array(
      ':bundle' => $entity_type,
    ));
    $having = TRUE;
  }
  $id_map['bundle'] = $sql_field;
  if (isset($this->entityConditions['bundle'])) {
    if (!empty($entity_info['entity keys']['bundle'])) {
      $this
        ->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['bundle'], $having);
    }
    else {

      // This entity has no bundle, so invalidate the query.
      $select_query
        ->where('1 = 0');
    }
  }

  // Order the query.
  foreach ($this->order as $order) {
    if ($order['type'] == 'entity') {
      $key = $order['specifier'];
      if (!isset($id_map[$key])) {
        throw new \EntityFieldQueryException(t('Do not know how to order on @key for @entity_type', array(
          '@key' => $key,
          '@entity_type' => $entity_type,
        )));
      }
      $select_query
        ->orderBy($id_map[$key], $order['direction']);
    }
    elseif ($order['type'] == 'property') {
      $select_query
        ->orderBy($base_table . '.' . $order['specifier'], $order['direction']);
    }
  }
  return $select_query;
}