Instalar y desinstalar una tabla para tu módulo en drupal

Dentro de la carpeta de tu módulo debes incluir un archivo con el nombre de tu módulo con extension .install (e.j. mimodulo.install).

En este archivo se va definir la estructura representativa para uno o más tablas con sus llaves relacionadas  e índices.  Esto es definido por hook_schema() (el prefijo hook debe ser reemplazado por el nombre de tu módulo en este caso mimodulo_schema() ).

 

<?php
function mimodulo_schema() {

$schema[‘stats_user_file’] = array(
‘description’ => ‘The base table for cies stats.’,
‘fields’ => array(
‘id’ => array(
‘description’ => ‘identifier for stats.’,
‘type’ => ‘int’,
‘not null’ => TRUE,
‘default’ => 0),
‘tid’ => array(
‘description’ => ‘identifier for a term.’,
‘type’ => ‘int’,
‘not null’ => TRUE,
‘default’ => 0),
‘uid’ => array(
‘description’ => ‘identifier for user.’,
‘type’ => ‘int’,
‘unsigned’ => TRUE,
‘not null’ => TRUE,
‘default’ => 0),
‘fid’ => array(
‘description’ => ‘identifier for file.’,
‘type’ => ‘int’,
‘not null’ => TRUE,
‘default’ => 0),
‘timestamp’ => array(
‘description’ => ‘timestamp UNIX’,
‘type’ => ‘int’,
‘not null’ => TRUE,
‘default’ => 0)
)
‘indexes’ => array(
‘tabla1_changed’        => array(‘changed’),
‘tabla1_created’        => array(‘created’),
),
‘unique keys’ => array(
‘id_tid’ => array(‘id’, ‘tid’),
‘uid’     => array(‘uid’)
),
‘primary key’ => array(‘id’)
);
return $schema;
}
?>

 

En http://drupal.org/node/146843 se puede ver la estructura de definición de un esquema con más detalles, tipo de datos y referencias.

Solo faltaría agregar hook_install() y hook_uninstall() para instalar y desinstalar respectivamente la tabla definida anteriormente

 

<?php

function mimodulo_schema() {
/* Definicion de la estructura de la tabla */
}

function mimodulo_install() {
// Crea mi tabla
drupal_install_schema(‘mimodulo’);
}

function mimodulo_uninstall() {
// Borra mi tabla
drupal_uninstall_schema(‘mimodulo’);
}

?>

Comments