public function Schema::fieldSetDefault

Same name in this branch
  1. 8.x drupal/core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::fieldSetDefault()
  2. 8.x drupal/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::fieldSetDefault()
  3. 8.x drupal/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php \Drupal\Core\Database\Driver\sqlite\Schema::fieldSetDefault()
  4. 8.x drupal/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::fieldSetDefault()

Set the default value for a field.

Parameters

$table: The table to be altered.

$field: The field to be altered.

$default: Default value to be set. NULL for 'default NULL'.

Throws

Drupal\Core\Database\SchemaObjectDoesNotExistException If the specified table or field doesn't exist.

Overrides Schema::fieldSetDefault

File

drupal/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php, line 369
Definition of Drupal\Core\Database\Driver\mysql\Schema

Class

Schema

Namespace

Drupal\Core\Database\Driver\mysql

Code

public function fieldSetDefault($table, $field, $default) {
  if (!$this
    ->fieldExists($table, $field)) {
    throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array(
      '@table' => $table,
      '@field' => $field,
    )));
  }
  if (!isset($default)) {
    $default = 'NULL';
  }
  else {
    $default = is_string($default) ? "'{$default}'" : $default;
  }
  $this->connection
    ->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $default);
}