You are here

public function DataTable::addField in Data 6

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

Add a field.

@todo: Check wether field name is available, otherwise change.

Throws

DataException

File

includes/DataTable.inc, line 247
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 addField($field, $spec) {
  $ret = array();
  db_add_field($ret, $this->name, $field, $spec);
  if ($ret[0]['success']) {
    $schema = $this->table_schema;
    $schema['fields'][$field] = $spec;
    $this
      ->update(array(
      'table_schema' => $schema,
    ));

    // @todo: use clearCaches().
    drupal_get_schema($this->name, TRUE);

    // Invalidate views caches to use new field immediately.
    if (function_exists('views_invalidate_cache')) {
      views_invalidate_cache();
    }
    return $field;
  }
  throw new DataException(t('Error adding field.'));
}