-
-
Save oksana-c/e5fa3cc88b0ddbd9c9ef4c799181ba4d to your computer and use it in GitHub Desktop.
(Drupal 8) Create a table in a database other than the default during module installation. Database must already be specified in settings.php.
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 d8module module. | |
*/ | |
function d8module_schema_otherdb() { | |
$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 d8module_install() { | |
\Drupal\Core\Database\Database::setActiveConnection('otherdb'); | |
$connection = \Drupal\Core\Database\Database::getConnection(); | |
$schema = d8module_schema_shared(); | |
foreach ($schema as $name => $table) { | |
$connection->schema()->createTable($name, $table); | |
} | |
\Drupal\Core\Database\Database::setActiveConnection(); | |
} | |
/** | |
* Implements hook_uninstall(). | |
*/ | |
function d8module_uninstall() { | |
\Drupal\Core\Database\Database::setActiveConnection('otherdb'); | |
$connection = \Drupal\Core\Database\Database::getConnection(); | |
$schema = d8module_schema_shared(); | |
foreach ($schema as $name => $table) { | |
$connection->schema()->dropTable($name); | |
} | |
\Drupal\Core\Database\Database::setActiveConnection(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment