You are here

public function DataTable::changeField in Data 6

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

Change a field.

Throws

DataException

File

includes/DataTable.inc, line 429
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) {
  $ret = array();

  // 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);
        }
      }
    }
  }
  db_change_field($ret, $this->name, $field, $field, $spec);
  if ($ret[0]['success']) {
    $schema = $this->table_schema;
    $schema['fields'][$field] = $spec;
    $this
      ->update(array(
      'table_schema' => $schema,
    ));
    drupal_get_schema($this->name, TRUE);
    return;
  }
  throw new DataException(t('Cannot change field.'));
}