You are here

public function DataTable::changeField in Data 7

Same name and namespace in other branches
  1. 6 includes/DataTable.inc \DataTable::changeField()

Change a field.

Throws

DataException

File

includes/DataTable.inc, line 449
Contains class definition for DataTable.

Class

DataTable
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.

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 {
    db_change_field($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);
}