In Drupal, every module uses at least two files.
- The
.info
file
- The
.module
file
If you are developing a custom module, you might also need a
.install
file, if you want to create or alter tables in database. Most modules use just one table so it will be difficult to understand what is wrong, in case you do what I did. Drupal has its own functions for database management. In no way mysql or pgsql functions must be used in a module. Drupal functions are everything you might need. Even if you find a function you need that only exists in mysql, try to create a port using drupal functions and php. It's the Drupal way and it is something you might benefit from later. A
.install
file uses at least three(3) functions.
- hook_schema()
- hook_install
- hook_uninstall
It's absolutely certain that creating the schema array won't be something difficult. Drupal core modules are more than great examples for this. In contrast to the words above,
hook_install()
and
hook_uninstall
can be sometimes confusing. Let's take a case where you need two tables and your module is named
example
. The first table will be named
example1
and the other
example2
. If you needed to create a mysql table you would possibly write:
$sql = "CREATE TABLE example1(id INT,...,...)";
mysql_query($sql) or die(mysql_error());
$sql = "CREATE TABLE example2(id INT,...,...)";
mysql_query($sql) or die(mysql_error());
I mean that since you have two tables to create, you will, most possibly, use two queries. I suppose the same goes for pgsql too. But not in Drupal. If you try to call
drupal_install_schema('example1')
or
drupal_install_schema('example2')
in
hook_install
all you will get(if you are lucky) is an error and it won't be very easy to discover what's really going on. The only thing sure is that no tables will be created in database. No! No need to start checking backward your changes to the
.install
file step by step! The solution is simple.
drupal_install_schema()
accepts as argument the name of the module and not the name we may want each table to have. To conclude:
CORRECT | FALSE |
/*
* Implements hook_install()
*/
function example_install(){
drupal_install_schema('example');
} | /*
* Implements hook_install()
*/
function example_install(){
drupal_install_schema('example1');
drupal_install_schema('example2');
}
|
Comments