Information for: DEVELOPERS   PARTNERS

Store data in a custom table

Fast Track to Drupal 8 Coding – Back to intro
Previous lesson - Define a custom config entity
Next lesson – Update an existing custom schema

Lesson Goal

Create a custom table to store data in the database.

Implementation Method

Drupal 8 and Drupal 7 are identical when defining a custom table, the only difference being the use of the Database service in Drupal 8.

Note

The db_insert/delete/update/query functions have been deprecated, and are now just wrappers until Drupal 9 is released.

Drupal Version Method
Drupal 7 Use hook_install() and hook_schema()
Drupal 8 Use hook_install() and hook_schema()

Drupal 8 method

As with Drupal 7, the module.install needs at the minimum the hook_schema() to create your table. The following example also implements the hook_install(), which is called after the hook_schema() to ensure that the table is created in advance.

/**
* Implements hook_install().
*
* Creates some default entries on this module custom table.
*
* @see hook_install()
*
* @ingroup lotus
*/
function lotus_install() {
  $database = \Drupal::database();
  // Add a default entry.
  $fields = array(
    'name' => 'John',
    'surname' => 'Doe',
    'age' => 0,
  );
  $database->insert('lotus')
    ->fields($fields)
    ->execute();
  // Add another entry.
  $fields = array(
    'name' => 'John',
    'surname' => 'Roe',
    'age' => 100,
    'uid' => 1,
  );
  $database->insert('lotus')
    ->fields($fields)
    ->execute();
}

/**
* Implements hook_schema().
*
* Defines the database tables used by this module.
*
* @see hook_schema()
*
* @ingroup lotus
*/
function lotus_schema() {
  $schema['lotus'] = array(
    'description' => 'Stores example person entries for demonstration purposes.',
    'fields' => array(
      'pid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique person ID.',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => "Creator user's {users}.uid",
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Name of the person.',
      ),
      'surname' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Surname of the person.',
      ),
      'age' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The age of the person in years.',
      ),
    ),
    'primary key' => array('pid'),
    'indexes' => array(
      'name' => array('name'),
      'surname' => array('surname'),
      'age' => array('age'),
    ),
  );

  return $schema;
}

Dependency Injection

The new method of using the Database system, and others, in Drupal 8:

https://www.drupal.org/docs/8/api/services-and-dependency-injection/services-and-dependency-injection-in-drupal-8

Dependency injection can only be done in a Class, which is why you see the usage of the service container helper in the hook_install example above. The structure of your module should use Classes, Controllers, and other similar patterns to utilize Dependency Injection to its fullest.