老葛的Drupal培训班 Think in Drupal
安装钩子函数一般将数据库表的安装委托给drupal_install_schema();而drupal_install_schema()负责从模块的模式钩子中获取模式定义,并修改数据库,如图5-3所示。接着,安装钩子函数再做一些其它的必需的安装工作。下面是来自modules/book/book.install文件的例子,这里将数据库表的安装委托给了drupal_install_schema()。由于书籍模块需要处理书籍节点类型,所以在安装完数据库表后它还创建了这一节点类型。
/**
* Implementation of hook_install().
*/
function book_install() {
// Create tables.
drupal_install_schema('book');
// Add the node type.
_book_install_type_create();
}
模式一般这样定义:
$schema['tablename'] = array(
// Table description.
'description' => t('Description of what the table is used for.'),
'fields' => array(
// Field definition.
'field1' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('Description of what this field is used for.'),
),
),
// Index declarations.
'primary key' => array('field1'),
);
图 5-3.使用模式定义创建数据库表