You are here

public function Table::changeField in Data 8

Change a field.

Throws

DataException

File

src/Table.php, line 447

Class

Table
Manages data access and manipulation for a single data table. Use data_create_table() or data_get_table() to instantiate an object from this class.

Namespace

Drupal\data

Code

public function changeField($field, $spec) {

  // If new type is text, check for PK and index restrictions.
  if ($spec['type'] == 'text') {
    if (in_array($field, $this->table_schema['primary key'])) {
      throw new DataException(t('Cannot make a primary key field a text field.'));
    }
    foreach ($this->table_schema['indexes'] as $index_name => $index) {
      foreach ($index as $index_field) {
        if (is_array($index_field)) {
          $index_field = array_shift($index_field);
        }
        if ($field == $index_field) {
          $this
            ->dropIndex($index_field);
        }
      }
    }
  }
  try {
    \Drupal::database()
      ->schema()
      ->changeField($this->name, $field, $field, $spec);
  } catch (DatabaseSchemaObjectDoesNotExistException $e) {
    throw new DataException(t('Cannot change field.'));
  }
  $schema = $this->table_schema;
  $schema['fields'][$field] = $spec;
  $this
    ->update(array(
    'table_schema' => $schema,
  ));
  drupal_get_schema($this->name, TRUE);
}