Neue Datenbanktabelle (Schema) nach Modul-Installation anlegen
Um neue Datenbanktabellen in Drupal zu erstellen, gibt es hook_schema()
. Dieser Hook wird jedoch nur ausgeführt, wenn das Modul installiert wird, bzw. deaktiviert, deinstalliert und neuinstalliert wird.
/** * Implements hook_schema(). */ function MYMODULE_schema() { $schema = array(); $schema['MYMODULE_MYTABLE'] = array( 'description' => 'Description of the table.', 'fields' => array( 'id' => array( 'description' => 'Field description', 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, ), ), ); return $schema; }
Deshalb kann man zusätzlich einen Update-Routine (hook_update_N()
) implementieren, wo das Schema nachträglich angewendet wird.
/** * Add the new table {mytable}. */ function MYMODULE_update_7101() { $schema = MYMODULE_schema(); $table = 'MYMODULE_MYTABLE'; if (!db_table_exists($table)) { db_create_table($table, $schema[$table]); } }
🔗 Links zu dieser Seite