Skip to content

Instantly share code, notes, and snippets.

@codexmas
Last active June 26, 2020 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save codexmas/733574476cb2e6ac770886e1c3075231 to your computer and use it in GitHub Desktop.
Save codexmas/733574476cb2e6ac770886e1c3075231 to your computer and use it in GitHub Desktop.
<?php
/**
* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment