function Select::addField

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select::addField()
  2. 8.9.x core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select::addField()
  3. 11.x core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select::addField()

Overrides SelectInterface::addField

2 calls to Select::addField()
Select::fields in core/lib/Drupal/Core/Database/Query/Select.php
Add multiple fields from the same table to be SELECTed.
Select::orderBy in core/modules/pgsql/src/Driver/Database/pgsql/Select.php
Overrides SelectQuery::orderBy().

File

core/lib/Drupal/Core/Database/Query/Select.php, line 538

Class

Select
Query builder for SELECT statements.

Namespace

Drupal\Core\Database\Query

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] = [
        'field' => $field,
        'table' => $table_alias,
        'alias' => $alias,
    ];
    return $alias;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.