You are here

public function EntityFieldQuery::finishQuery in RESTful 7.2

Overrides EntityFieldQuery::finishQuery

1 call to EntityFieldQuery::finishQuery()
EntityFieldQuery::buildQuery in src/Util/EntityFieldQuery.php
Builds the SelectQuery and executes finishQuery().
1 method overrides EntityFieldQuery::finishQuery()
EntityFieldQuery::finishQuery in src/Util/EntityFieldQuery.php

File

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

Class

EntityFieldQuery

Namespace

Drupal\restful\Util

Code

public function finishQuery($select_query, $id_key = 'entity_id') {
  $entity_type = $this->entityConditions['entity_type']['value'];
  foreach ($this
    ->getRelationships() as $delta => $relationship) {

    // A relational filter consists of a chain of relationships and a value
    // for a condition at the end.
    // Relationships start with the entity base table.
    $entity_info = entity_get_info($entity_type);
    $entity_table = $entity_table_alias = $entity_info['base table'];

    // Add the table if the base entity table was not added because:
    // 1. There was a fieldCondition or fieldOrderBy, AND
    // 2. There was no property condition or order.
    if ($delta == 0) {
      $is_entity_table_present = FALSE;
      $field_base_table_alias = NULL;
      foreach ($select_query
        ->getTables() as $table_info) {

        // Search for the base table and check if the entity table is present
        // for the resource's entity type.
        if (!$field_base_table_alias && empty($table_info['join type'])) {
          $field_base_table_alias = $table_info['alias'];
        }
        if ($table_info['table'] == $entity_table) {
          $is_entity_table_present = TRUE;
          break;
        }
      }
      if (!$is_entity_table_present && $field_base_table_alias) {

        // We have the base table and we need to join it to the entity table.
        _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table_alias);
      }
    }

    // Pop the last item, since it is the one that has to match the filter and
    // will have the WHERE associated.
    $condition = array_pop($relationship['relational_filters']);
    foreach ($relationship['relational_filters'] as $relational_filter) {

      /* @var RelationalFilterInterface $relational_filter */
      if ($relational_filter
        ->getType() == RelationalFilterInterface::TYPE_FIELD) {
        $field_table_name = _field_sql_storage_tablename(field_info_field($relational_filter
          ->getName()));
        $field_table_alias = $this::aliasJoinTable($field_table_name, $select_query);
        $select_query
          ->addJoin('INNER', $field_table_name, $field_table_alias, sprintf('%s.%s = %s.%s', $entity_table_alias, $entity_info['entity keys']['id'], $field_table_alias, $id_key));

        // Get the entity type being referenced.
        $entity_info = entity_get_info($relational_filter
          ->getEntityType());
        $entity_table_alias = $this::aliasJoinTable($entity_info['base table'], $select_query);
        $select_query
          ->addJoin('INNER', $entity_info['base table'], $entity_table_alias, sprintf('%s.%s = %s.%s', $field_table_name, _field_sql_storage_columnname($relational_filter
          ->getName(), $relational_filter
          ->getColumn()), $entity_table_alias, $relational_filter
          ->getTargetColumn()));
      }
      elseif ($relational_filter
        ->getType() == RelationalFilterInterface::TYPE_PROPERTY) {

        // In this scenario we want to join with the new table entity. This
        // will only work if the property contains the referenced entity ID
        // (which is not unreasonable).
        $host_entity_table = $entity_table_alias;
        $entity_info = entity_get_info($relational_filter
          ->getEntityType());
        $entity_table_alias = $this::aliasJoinTable($entity_info['base table'], $select_query);
        $select_query
          ->addJoin('INNER', $entity_info['base table'], $entity_table_alias, sprintf('%s.%s = %s.%s', $host_entity_table, $relational_filter
          ->getName(), $entity_table_alias, $relational_filter
          ->getTargetColumn()));
      }
    }

    /* @var RelationalFilterInterface $condition */
    if ($condition
      ->getType() == RelationalFilterInterface::TYPE_FIELD) {

      // Make the join to the filed table for the condition.
      $field_table_name = _field_sql_storage_tablename(field_info_field($condition
        ->getName()));
      $field_column = _field_sql_storage_columnname($condition
        ->getName(), $condition
        ->getColumn());
      $field_table_alias = $this::aliasJoinTable($field_table_name, $select_query);
      $select_query
        ->addJoin('INNER', $field_table_name, $field_table_alias, sprintf('%s.%s = %s.%s', $entity_table_alias, $entity_info['entity keys']['id'], $field_table_alias, $id_key));
      if (in_array($relationship['operator'], array(
        'IN',
        'BETWEEN',
      ))) {
        $select_query
          ->condition($field_table_name . '.' . $field_column, $relationship['value'], $relationship['operator'][0]);
      }
      else {
        for ($index = 0; $index < count($relationship['value']); $index++) {
          $select_query
            ->condition($field_table_name . '.' . $field_column, $relationship['value'][$index], $relationship['operator'][$index]);
        }
      }
    }
    elseif ($condition
      ->getType() == RelationalFilterInterface::TYPE_PROPERTY) {
      if (in_array($relationship['operator'], array(
        'IN',
        'BETWEEN',
      ))) {
        $select_query
          ->condition($entity_table_alias . '.' . $condition
          ->getName(), $relationship['value'], $relationship['operator'][0]);
      }
      else {
        for ($index = 0; $index < count($relationship['value']); $index++) {
          $select_query
            ->condition($entity_table_alias . '.' . $condition
            ->getName(), $relationship['value'][$index], $relationship['operator'][$index]);
        }
      }
    }
  }
  return parent::finishQuery($select_query, $id_key);
}