Same name and namespace in other branches
  1. 6.x-3.x plugins/views_plugin_query_default.inc \views_plugin_query_default::add_field()

Add a field to the query table, possibly with an alias. This will automatically call ensure_table to make sure the required table exists, *unless* $table is unset.

Parameters

string $table: The table this field is attached to. If NULL, it is assumed this will be a formula; otherwise, ensure_table is used to make sure the table exists.

string $field: The name of the field to add. This may be a real field or a formula.

string $alias: The alias to create. If not specified, the alias will be $table_$field unless $table is NULL. When adding formulae, it is recommended that an alias be used.

array $params: An array of parameters additional to the field that will control items such as aggregation functions and DISTINCT.

Return value

string The name that this field can be referred to as, usually the alias.

2 calls to views_plugin_query_default::add_field()
views_plugin_query_default::add_orderby in plugins/views_plugin_query_default.inc
Add an ORDER BY clause to the query.
views_plugin_query_default::query in plugins/views_plugin_query_default.inc
Generate a query and a countquery from all of the information supplied to the object.

File

plugins/views_plugin_query_default.inc, line 805
Definition of views_plugin_query_default.

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

public function add_field($table, $field, $alias = '', $params = array()) {

  // We check for this specifically because it gets a special alias.
  if ($table == $this->base_table && $field == $this->base_field && empty($alias)) {
    $alias = $this->base_field;
  }
  if ($table && empty($this->table_queue[$table])) {
    $this
      ->ensure_table($table);
  }
  if (!$alias && $table) {
    $alias = $table . '_' . $field;
  }

  // Make sure an alias is assigned.
  $alias = $alias ? $alias : $field;

  // PostgreSQL truncates aliases to 63 characters.
  // @see http://drupal.org/node/571548
  // We limit the length of the original alias up to 60 characters
  // to get a unique alias later if its have duplicates.
  $alias = strtolower(substr($alias, 0, 60));

  // Create a field info array.
  $field_info = array(
    'field' => $field,
    'table' => $table,
    'alias' => $alias,
  ) + $params;

  // Test to see if the field is actually the same or not. Due to differing
  // parameters changing the aggregation function, we need to do some
  // automatic alias collision detection.
  $base = $alias;
  $counter = 0;
  while (!empty($this->fields[$alias]) && $this->fields[$alias] != $field_info) {
    $field_info['alias'] = $alias = $base . '_' . ++$counter;
  }
  if (empty($this->fields[$alias])) {
    $this->fields[$alias] = $field_info;
  }

  // Keep track of all aliases used.
  $this->field_aliases[$table][$field] = $alias;
  return $alias;
}