You are here

public function DataTable::adopt in Data 7

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

Let Data manage a table that already exists in the database.

Uses the $name property of the object to determine which database table to adopt.

Return value

TRUE if the table was successfully adopted; FALSE if the table was already known to Data, if the query failed, or if Schema isn't available.

File

includes/DataTable.inc, line 146
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 adopt() {
  if ($this
    ->defined() || !module_exists('schema')) {
    return FALSE;
  }
  $schema = schema_dbobject()
    ->inspect(variable_get('schema_database_connection', 'default'), $this->name);
  if (isset($schema[$this->name])) {
    $table = array(
      'name' => $this->name,
      'title' => data_natural_name($this->name),
      'table_schema' => $schema[$this->name],
      // Add in an empty meta array with the field names so other modules can rely on it.
      'meta' => array(
        'fields' => array_fill_keys(array_keys($schema[$this->name]['fields']), array()),
      ),
    );
    if (drupal_write_record('data_tables', $table)) {

      // Clear caches.
      $this
        ->clearCaches();
      return TRUE;
    }
  }

  // Clear caches.
  $this
    ->clearCaches();
  return FALSE;
}