The Schema API
The purpose of the Schema API is to allow the defining of database table structures in PHP and to have Drupal interact with the database engine and turn those definitions into a reality. Apart from the fact that we don't ever have to see things such as CREATE TABLE
, we ensure that our table structures can be applied to multiple types of databases. If you remember in Chapter 1, Developing for Drupal 9, I mentioned that Drupal can work with MySQL, PostgreSQL, SQLite, and others, if they support PDO, so the Schema API ensures this cross-compatibility.
The central component of the Schema API is hook_schema()
. This is used to provide the initial table definitions of a given module. Implementations of this hook belong in the *.install
file of the module and are fired when the module is first installed. If alterations need to be made to existing database tables, there are a number of methods that can be used inside update hooks to make these changes.
In this section...