You are here

function Sql::compile_fields in Views (for Drupal 7) 8.3

Adds fields to the query.

Parameters

Drupal\Core\Database\Query\SelectInterface $query: The drupal query object.

1 call to Sql::compile_fields()
Sql::query in lib/Drupal/views/Plugin/views/query/Sql.php
Generate a query and a countquery from all of the information supplied to the object.

File

lib/Drupal/views/Plugin/views/query/Sql.php, line 1189
Definition of Drupal\views\Plugin\views\query\Sql.

Class

Sql
@todo.

Namespace

Drupal\views\Plugin\views\query

Code

function compile_fields($query) {
  foreach ($this->fields as $field) {
    $string = '';
    if (!empty($field['table'])) {
      $string .= $field['table'] . '.';
    }
    $string .= $field['field'];
    $fieldname = !empty($field['alias']) ? $field['alias'] : $string;
    if (!empty($field['count'])) {

      // Retained for compatibility.
      $field['function'] = 'count';
    }
    if (!empty($field['function'])) {
      $info = $this
        ->get_aggregation_info();
      if (!empty($info[$field['function']]['method']) && is_callable(array(
        $this,
        $info[$field['function']]['method'],
      ))) {
        $string = $this::$info[$field['function']]['method']($field['function'], $string);
        $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
        $query
          ->addExpression($string, $fieldname, $placeholders);
      }
      $this->has_aggregate = TRUE;
    }
    elseif (empty($field['table'])) {
      $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
      $query
        ->addExpression($string, $fieldname, $placeholders);
    }
    elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
      $query
        ->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
    }
    elseif (empty($field['aggregate'])) {
      $query
        ->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
    }
    if ($this->get_count_optimized) {

      // We only want the first field in this case.
      break;
    }
  }
}