我计划在hook_schema的帮助下添加一个表.我已经定义了模式,并在我的模块中调用它.但是,没有创建架构.我哪里错了.我的sample2.install文件中有以下代码
function sample2_schema(){
$schema['mytable1'] = array(
'description' => t('My table description'),
'fields' => array(
'mycolumn1' => array(
'description' => t('My unique identifier'),
'type' => 'serial',
'unsigned' => true,
'not null' => true,
),
'myvarchar' => array(
'description' => t('My varchar'),
'type' => 'varchar',
'length' => 32,
'not null' => true,
),
'mytimestamp' => array(
'description' => t('My timestamp'),
'type' => 'int',
'not null' => true,
),
),
'indexes' => array(
'myvarchar' => array('myvarchar'),
),
'primary key' => array('mycolumn1'),
'unique keys' => array(
'mycolumn1' => array('mycolumn1'),
),
);
return $schema;
}
function sample2_install(){
drupal_install_schema('sample2');
}
function sample2_uninstall(){
drupal_uninstall_schema('sample2');
}
Run Code Online (Sandbox Code Playgroud)
要调用它,我在sample2.module文件中使用以下代码.
$record = (object) NULL;
$record->myvarchar = 'blah';
$record->mytimestamp = strtotime('now');
drupal_write_record('mytable1',$record);
Run Code Online (Sandbox Code Playgroud)
这不会创建任何表.
如果在添加sample2_schema()和sample2_install()函数之前存在并启用了sample2模块,则需要将其卸载(不仅仅是禁用),然后再次安装/启用它.否则,Drupal不会使用你的hook_install().另一种解决方案是在hook_update_N()实现中安装模式.然后使用u pdate.php或drush updatedb运行更新代码.
function sample2_update_6001() {
drupal_install_schema('sample2');
}
Run Code Online (Sandbox Code Playgroud)