创建数据库表(1)

老葛的Drupal培训班 Think in Drupal

让我们看一下Drupal的书籍模块中的模式定义,位于modules/book/book.install文件中:
/**
* Implementation of hook_schema().
*/
function book_schema() {
    $schema['book'] = array(
        'description' => t('Stores book outline information. Uniquely connects each node in the outline to a link in {menu_links}'),
        'fields' => array(
            'mlid' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
                'description' => t("The book page's {menu_links}.mlid."),
            ),
            'nid' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
                'description' => t("The book page's {node}.nid."),
            ),
            'bid' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
                'description' => t("The book ID is the {book}.nid of the top-level                  page."),
            ),
        ),
 
        'primary key' => array('mlid'),
        'unique keys' => array(
            'nid' => array('nid'),
        ),
        'indexes' => array(
            'bid' => array('bid'),
        ),
    );
 
    return $schema;
}
 
    这个模式定义描述了book表,它包含3个int类型的字段。它还有一个主键,一个唯一索引(这意味着该字段中的所有条目都是唯一的)和一个普通索引。注意,在字段描述中,引用另一个表中的字段时,需要为其使用花括号。这样模式模块(参看下一节)可以为表的描述构建方便的超链接。
 

Drupal版本: