Welcome back to the "Module Development" tutorial series. In this lesson, we will explore how to create database tables in Drupal using the Schema API. This process allows custom modules to define their database tables, which Drupal creates or updates as necessary. By the end of this lesson, you will be equipped with the knowledge to set up tables using your module's .install
file.
Understanding the .install File
In Drupal, the .install
file is pivotal for performing tasks that are required once a module gets installed. These tasks typically include setting up database schemas, adding configuration variables, and other setup actions. As we move forward, we will primarily focus on using the .install
file for database schema setup.
Getting Started with Schema API
Before you start, ensure you have a basic understanding of PHP and the structure of a Drupal module. The Schema API is a part of the Drupal core which allows developers to define the structure of database tables.
Defining a Database Table
Firstly, navigate to the custom module directory and find the .install
file. If it doesn't exist, create a new one using the module's machine name, such as example_module.install
.
Within this file, you can define a hook implementation to specify your table schema. Here's how we can create a simple table named example_table
:
<?php
/**
* Implements hook_schema().
*/
function example_module_schema() {
$schema['example_table'] = [
'description' => 'Stores example data.',
'fields' => [
'id' => [
'description' => 'Primary Key: Unique identifier for each entry.',
'type' => 'serial',
'not null' => TRUE,
],
'name' => [
'description' => 'Name of the example entry.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'created' => [
'description' => 'The time that the example was created.',
'type' => 'int',
'size' => 'normal',
'not null' => TRUE,
],
],
'primary key' => ['id'],
];
return $schema;
}
?>
Explanation of the Schema Definitions
- Fields: These determine the columns of your database table. Each field includes keys such as 'type', 'length', and 'not null' to specify the column's properties.
- Primary Key: Specifies the column that uniquely identifies each row in the table. In our example, the
id
field is set as the primary key. - Type: Defines the data type of the field, such as 'serial', 'varchar', or 'int'.
- Description: Provides a human-readable description for each field, helpful for future reference and collaboration.
Creating and Updating the Table
Once you've defined the schema, run the following Drush command to rebuild the site cache and install the schema:
drush cache-rebuild && drush pm-enable example_module
If you are updating the schema in an existing module, you'll use:
drush updatedb
This command will apply the schema changes defined in your .install
file.
Testing the Schema Installation
To ensure your table is created correctly, verify your database directly using a tool like PhpMyAdmin or a command-line interface like MySQL CLI. Check if the table named example_table
exists and that it includes all defined fields.
Conclusion
Excellent work! You've successfully learned how to define and create database tables in Drupal using the Schema API within the .install
file of your module. By following these steps, you can leverage the full potential of Drupal's powerful integration with databases.
What's Next?
In the next lesson, we'll delve into using Database::getConnection()
for running queries. This will build upon our understanding of how to interact with the database, providing greater flexibility and functionality within your custom Drupal modules. Stay tuned!