Создать таблицу в базе данных можно несколькими путями.
При установке модуля, файл module_name.install
<?php
/**
* Implements hook_schema().
*/
function module_name_schema() {
$schema = [];
$schema['table_name'] = [
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'ID',
],
'integer' => [
'type' => 'int',
'unsigned' => TRUE,
'size' => 'medium',
'not null' => TRUE,
'description' => 'Integer',
],
'varchar' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 0,
'description' => 'Varchar',
],
'created' => [
'description' => 'Unixtimestamp created.',
'type' => 'int',
'unsigned' => TRUE,
'disp-size' => 11,
],
'changed' => [
'description' => 'Unixtimestamp changed.',
'type' => 'int',
'unsigned' => TRUE,
'disp-size' => 11,
],
],
'primary key' => ['id'],
];
return $schema;
}
Создание таблицы из модуля:
<?php
use \Drupal\Core\Database\Database;
$table = [
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'ID',
],
'created' => [
'description' => 'Unixtimestamp created.',
'type' => 'int',
'unsigned' => TRUE,
'disp-size' => 11,
],
'changed' => [
'description' => 'Unixtimestamp changed.',
'type' => 'int',
'unsigned' => TRUE,
'disp-size' => 11,
],
],
'primary key' => ['id'],
];
$schema = Database::getConnection()->schema();
$schema->createTable('NAME_TABLE', $table);
Удаление таблицы из модуля:
<?php
use \Drupal\Core\Database\Database;
$schema = Database::getConnection()->schema();
$schema->dropTable('NAME_TABLE');