Monday, September 26, 2011

Drupal 6 Schema

Drupal Schema API allows to declare their database tables in a structured array. And provides API functions for creating, dropping, and changing tables, columns, keys, and indexes in a table.


Create "module.install" file under the module.

Create the following hook function to add schema,

hook_schema()
hook_install()
hook_uninstall()

// Define a table
function hook_shema(){
// Define table array
For example:
$schema['node'] = array(
'fields' => array(
'nid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => t('Primary Key'),
),
'status' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('Status.'),
),
),
'primary key' => array('nid'),
);
}

// Install all the tables while enable the module form list
function hook_install() {
drupal_install_schema(hook);
}

// Drop the all tables while uninstall the module
function hook_uninstall() {
drupal_uninstall_schema(hook);
}

// Add one row to table
drupal_write_record(table_name, $record_object);

// Update a row in a table
drupal_write_record(table_name, $record_object, primary_key);

No comments:

Post a Comment