Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wiratama/ee534bd3f68c510b05246b3754b38897 to your computer and use it in GitHub Desktop.
Save wiratama/ee534bd3f68c510b05246b3754b38897 to your computer and use it in GitHub Desktop.
Create custom table in drupal 8
Creating custom table in drupal 8 is not recommended. Create entity types instead.
But if you want to Create custom table you can create either by implementing hook_schema or
else create schema file config/schema/your_custom_module_name.schema.yml in your module
(https://www.drupal.org/docs/8/api/configuration-api/configuration-schemametadata).
( https://drupal.stackexchange.com/questions/219580/best-practice-for-creating-table-in-custom-module )
1. Implementing hook_schema in .module file (Not recommended).
/**
* Implements hook_schema().
*
* @todo to be removed this as this is already declared in inactive.install file(confirm)
*
*/
function inactive_user_schema() {
$schema['inactive_users'] = array(
'description' => 'The base table for inactive_users.',
'fields' => array(
'uid' => array(
'description' => 'The primary identifier for a user.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'notified_admin' => array(
'description' => 'Admin notifier.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'notified_user' => array(
'description' => 'User notifier.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'warned_user_block_timestamp' => array(
'description' => 'Timestamp warning.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'notified_user_block' => array(
'description' => 'User block warning.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'notified_admin_block' => array(
'description' => 'Timestamp warning.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'warned_user_delete_timestamp' => array(
'description' => 'Timestamp warning.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'protected' => array(
'description' => 'Timestamp warning.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'inactive_user_notification_flag' => array(
'description' => 'Inactive user notification flag.',
'type' => 'int',
'not null' => TRUE,
'default' => 1,
),
),
'primary key' => array('uid'),
);
$schema['inactive_user_flag'] = array(
'description' => 'Inactive user flag.',
'fields' => array(
'id' => array(
'description' => 'The primary key to store unique information.',
'type' => 'serial',
'not null' => FALSE,
),
'user_id' => array(
'description' => 'Storing inactive user id.',
'type' => 'int',
),
'value' => array(
'description' => 'Storing inacitve user value.',
'type' => 'int',
),
),
'primary key' => array('id'),
);
return $schema;
}
2. other way you have to schema.yml inside config/schema/ follow this link https://www.drupal.org/docs/8/api/configuration-api/configuration-schemametadata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment