Schema API 主要是用来建立表,可以参考hook_schema(),经常用在模块开发中,使用在modulename.install文件里。
hook_schema() 返回一系列数组,主要是关于表的定义,以及表字段等。
可以参考 http://api.drupal.org/api/function/hook_schema/6
如何来自己定义个表,可以具体使用hook_schema来实现。如下
'The base table for nodes.',//表的描述
'fields' => array(//然后定义表字段的数组
'nid' => array(//定义一个nid字段
'description' => 'The primary identifier for a node.',//字段描述,
'type' => 'serial',//字段类型 serial 连续递增 整数型int
// 'type': 字段的数据类型 'varchar', 'int', 'serial' 'float', 'numeric', 'text', 'blob' or 'datetime'.
'unsigned' => TRUE,
'not null' => TRUE), // 默认为假flase * 'not null': 如果为真,次列不容许空值插入,默认为假
'vid' => array( // 定义一个vid 字段
'description' => 'The current {node_revisions}.vid version identifier.',
'type' => 'int', // 次字段为整数型
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0), //默认值为零
'type' => array(
'description' => 'The {node_type} of this node.',
'type' => 'varchar', // 字符串类型
'length' => 32,//长度为32b 字段类型为'varchar' or 'text' 时定义
'not null' => TRUE,
'default' => ''),
'title' => array(
'description' => 'The title of this node, always treated a non-markup plain text.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''),
),
'indexes' => array(
'node_changed' => array('changed'),
'node_created' => array('created'),
),
'unique keys' => array(
'nid_vid' => array('nid', 'vid'),
'vid' => array('vid')
),
'primary key' => array('nid'),//主键
);
return $schema;//返回数组
}
?>
关于schema里面的参数定义 http://api.drupal.org/api/group/schemaapi/6
评论
发表新评论