Created
September 27, 2015 19:08
-
-
Save laradevitt/b3b9f04116b65572e684 to your computer and use it in GitHub Desktop.
(Drupal 7) Create a table in a database other than the default during module installation. Database must already be specified in settings.php. Original source: http://drupal.stackexchange.com/a/51673/28700
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Install, update and uninstall functions for the d7module module. | |
*/ | |
function d7module_schema_shared() { | |
$schema['mytable'] = array( | |
'description' => 'My table description.', | |
'fields' => array( | |
'myfield' => array( | |
'description' => 'My field description.', | |
'type' => 'serial', | |
'size' => 'medium', | |
'not null' => TRUE, | |
'unsigned' => TRUE, | |
), | |
), | |
'primary key' => array('myfield'), | |
); | |
return $schema; | |
} | |
/** | |
* Implements hook_install(). | |
*/ | |
function d7module_install() { | |
db_set_active('otherdb'); | |
$schema = d7module_schema_shared(); | |
foreach ($schema as $name => $table) { | |
db_create_table($name, $table); | |
} | |
db_set_active(); | |
} | |
/** | |
* Implements hook_uninstall(). | |
*/ | |
function d7module_uninstall() { | |
db_set_active('otherdb'); | |
$schema = d7module_schema_shared(); | |
foreach ($schema as $name => $table) { | |
db_drop_table($name); | |
} | |
db_set_active(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment