public function Update::__toString

public Update::__toString()

Implements PHP magic __toString method to convert the query to a string.

Return value

string The prepared statement.

Overrides Query::__toString

File

core/lib/Drupal/Core/Database/Query/Update.php, line 157

Class

Update
General class for an abstracted UPDATE operation.

Namespace

Drupal\Core\Database\Query

Code

public function __toString() {
  // Create a sanitized comment string to prepend to the query.
  $comments = $this->connection->makeComment($this->comments);

  // Expressions take priority over literal fields, so we process those first
  // and remove any literal fields that conflict.
  $fields = $this->fields;
  $update_fields = array();
  foreach ($this->expressionFields as $field => $data) {
    if ($data['expression'] instanceof SelectInterface) {
      // Compile and cast expression subquery to a string.
      $data['expression']->compile($this->connection, $this);
      $data['expression'] = ' (' . $data['expression'] . ')';
    }
    $update_fields[] = $this->connection->escapeField($field) . '=' . $data['expression'];
    unset($fields[$field]);
  }

  $max_placeholder = 0;
  foreach ($fields as $field => $value) {
    $update_fields[] = $this->connection->escapeField($field) . '=:db_update_placeholder_' . ($max_placeholder++);
  }

  $query = $comments . 'UPDATE {' . $this->connection->escapeTable($this->table) . '} SET ' . implode(', ', $update_fields);

  if (count($this->condition)) {
    $this->condition->compile($this->connection, $this);
    // There is an implicit string cast on $this->condition.
    $query .= "\nWHERE " . $this->condition;
  }

  return $query;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Query!Update.php/function/Update::__toString/8.1.x