1.20.x select.inc public SelectQuery::addField($table_alias, $field, $alias = NULL)

Implements SelectQueryInterface::addField().

Overrides SelectQueryInterface::addField

File

includes/database/select.inc, line 1451

Class

SelectQuery
Query builder for SELECT statements.

Code

public function addField($table_alias, $field, $alias = NULL) {
  // If no alias is specified, first try the field name itself.
  if (empty($alias)) {
    $alias = $field;
  }

  // If that's already in use, try the table name and field name.
  if (!empty($this->fields[$alias])) {
    $alias = $table_alias . '_' . $field;
  }

  // If that is already used, just add a counter until we find an unused alias.
  $alias_candidate = $alias;
  $count = 2;
  while (!empty($this->fields[$alias_candidate])) {
    $alias_candidate = $alias . '_' . $count++;
  }
  $alias = $alias_candidate;

  $this->fields[$alias] = array(
    'field' => $field,
    'table' => $table_alias,
    'alias' => $alias,
  );

  return $alias;
}