gpt4 book ai didi

drupal - 使用 hook_schema() 添加表

转载 作者:行者123 更新时间:2023-12-05 00:05:23 26 4
gpt4 key购买 nike

我计划在 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');
}

为了调用它,我在我的 sample2.module 文件中使用了以下代码。
        $record = (object) NULL;
$record->myvarchar = 'blah';
$record->mytimestamp = strtotime('now');
drupal_write_record('mytable1',$record);

这不会创建任何表。

最佳答案

如果 sample2 模块存在并在添加 sample2_schema() 之前启用和 sample2_install()功能,您需要卸载它(不仅仅是禁用),然后再次安装/启用它。否则,Drupal 不会使用你的 hook_install() .另一种解决方案是将架构安装在 hook_update_N() 中。执行。然后用你pdate.phpdrush updatedb运行更新代码。

function sample2_update_6001() {
drupal_install_schema('sample2');
}

关于drupal - 使用 hook_schema() 添加表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4550249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com