function views_query::add_field

2.x query.inc views_query::add_field($table, $field, $alias = '', $params = NULL)

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

$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.

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

$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.

Return value

$name The name that this field can be referred to as. Usually this is the alias.

1 call to views_query::add_field()
views_query::add_orderby in includes/query.inc
Add an ORDER BY clause to the query.

File

includes/query.inc, line 627
query.inc Defines the query object which is the underlying layer in a View.

Class

views_query
Object used to create a SELECT query.

Code

function add_field($table, $field, $alias = '', $params = NULL) {
  // 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;
  }

  $name = $alias ? $alias : $field;

  // @todo FIXME -- $alias, then $name is inconsistent
  if (empty($this->fields[$alias])) {
    $this->fields[$name] = array(
      'field' => $field,
      'table' => $table,
      'alias' => $alias,
    );
  }

  foreach ((array) $params as $key => $value) {
    $this->fields[$name][$key] = $value;
  }

  return $name;
}