Community Documentation

drupal_get_schema

6 common.inc drupal_get_schema($table = NULL, $rebuild = FALSE)
7 bootstrap.inc drupal_get_schema($table = NULL, $rebuild = FALSE)
8 bootstrap.inc drupal_get_schema($table = NULL, $rebuild = FALSE)

Get the schema definition of a table, or the whole database schema.

The returned schema will include any modifications made by any module that implements hook_schema_alter().

Parameters

$table: The name of the table. If not given, the schema of all tables is returned.

$rebuild: If true, the schema will be rebuilt instead of retrieved from the cache.

Related topics

▾ 2 functions call drupal_get_schema()

drupal_schema_fields_sql in includes/common.inc
Retrieve a list of fields from a table schema. The list is suitable for use in a SQL query.
drupal_write_record in includes/common.inc
Save a record to the database based upon the schema.

File

includes/common.inc, line 3297
Common functions that many Drupal modules will need to reference.

Code

<?php
function drupal_get_schema($table = NULL, $rebuild = FALSE) {
  static $schema = array();

  if (empty($schema) || $rebuild) {
    // Try to load the schema from cache.
    if (!$rebuild && $cached = cache_get('schema')) {
      $schema = $cached->data;
    }
    // Otherwise, rebuild the schema cache.
    else {
      $schema = array();
      // Load the .install files to get hook_schema.
      module_load_all_includes('install');

      // Invoke hook_schema for all modules.
      foreach (module_implements('schema') as $module) {
        // Cast the result of hook_schema() to an array, as a NULL return value
        // would cause array_merge() to set the $schema variable to NULL as well.
        // That would break modules which use $schema further down the line.
        $current = (array) module_invoke($module, 'schema');
        _drupal_initialize_schema($module, $current);
        $schema = array_merge($schema, $current);
      }

      drupal_alter('schema', $schema);
      cache_set('schema', $schema);
    }
  }

  if (!isset($table)) {
    return $schema;
  }
  elseif (isset($schema[$table])) {
    return $schema[$table];
  }
  else {
    return FALSE;
  }
}
?>

Comments

Schema must be altered in module.install?

Hi, I'm trying to customize a module by adding extra fields to a table and saving them by adding them into the mod's $form.

But it refuses to save. On code inspection, it seems that drupal_get_schema is called to get an array of fields to save to. I tried TRUE for $rebuild but it didn't work too.

After reading this doc, can I confirm that drupal builds the schema based on all the .install files? If you add a field to a table, it will not be reflected unless there's a corresponding entry in the .install file?

Get Schema

Apparently the answer is yes. drupal_get_schema('db', 1) after I modified the .install file's db declarations. The 1 inside the function is just to ensure it doesn't build from cache, can remove after you're done with the modifications.

Kindly correct me if I'm wrong.

Better not to add fields

drupal_write_record() is used widely in the drupal core to store data, and it looks at the schema defined in the .install profiles to build the database query. If you are changing the database tables without updating the schema in the .install files, these calls to drupal_write_record() may start failing. If any of the new table columns you're adding is required and has not a default value, they will surely fail.

My advice here is not to add fields to a Drupal existing table. Better if you create a new relation with the columns you want to add.

Much better, if you are modifying node fields you can always use cck. A more drupalistic way ;)

Login or register to post comments