8.3.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = [])
8.3.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = [])
8.0.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = array())
8.0.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = array())
8.1.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = array())
8.1.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = array())
8.2.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = array())
8.2.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = array())
8.4.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = [])
8.4.x Select.php public Select::addExpression($expression, $alias = NULL, $arguments = [])

Adds an expression to the list of "fields" to be SELECTed.

An expression can be any arbitrary string that is valid SQL. That includes various functions, which may in some cases be database-dependent. This method makes no effort to correct for database-specific functions.

Parameters

$expression: The expression string. May contain placeholders.

$alias: The alias for this expression. If not specified, one will be generated automatically in the form "expression_#". The alias will be checked for uniqueness, so the requested alias may not be the alias that is assigned in all cases.

$arguments: Any placeholder arguments needed for this expression.

Return value

The unique alias that was assigned for this expression.

Overrides SelectInterface::addExpression

1 call to Select::addExpression()
Select::orderRandom in core/lib/Drupal/Core/Database/Query/Select.php
Orders the result set by a random value.
1 method overrides Select::addExpression()
Select::addExpression in core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
Adds an expression to the list of "fields" to be SELECTed.

File

core/lib/Drupal/Core/Database/Query/Select.php, line 544
Contains \Drupal\Core\Database\Query\Select.

Class

Select
Query builder for SELECT statements.

Namespace

Drupal\Core\Database\Query

Code

public function addExpression($expression, $alias = NULL, $arguments = array()) {
  if (empty($alias)) {
    $alias = 'expression';
  }

  $alias_candidate = $alias;
  $count = 2;
  while (!empty($this->expressions[$alias_candidate])) {
    $alias_candidate = $alias . '_' . $count++;
  }
  $alias = $alias_candidate;

  $this->expressions[$alias] = array(
    'expression' => $expression,
    'alias' => $alias,
    'arguments' => $arguments,
  );

  return $alias;
}

Comments

sime’s picture

If you get an error like exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'foo' in 'field list' then check that you're using single quotes inside your expression. MySQL is happy with double quotes but PDO doesn't like it.