我有一个模块和一个用于创建表的安装文件。

根据我的要求,我需要在同一安装文件中添加另一个表。我知道通过使用 hook_schema() 我们可以创建n个表数。
但是,我们需要卸载并重新安装以创建表。如果我使用它,我的现有数据将丢失,因此我不应该这样做。

有什么方法可以在不卸载和重新安装模块的情况下创建表?

有帮助吗?

解决方案

这个链接回答了我的问题: 更新表:hook_update_n()函数.

例如:

在Drupal 7中:

<?php
/**
 * Create new database table {mytable2}.
 */
function mymodule_update_7101() {
  $schema['mytable2'] = array(
     // table definition array goes here
  );
  db_create_table('mytable2', $schema['mytable2']);
}
?>

在Drupal 6中:

<?php
/**
 * Create new database table {mytable2}.
 */
function mymodule_update_6101() {
  $schema['mytable2'] = array(
     // table definition array goes here
  );
  $ret = array();
  db_create_table($ret, 'mytable2', $schema['mytable2']);
  return $ret;
}
?>

其他提示

hook_install() 仅在首次安装模块时才称为;在那之后, hook_install() 仅当您首先卸载模块时,才会被调用。

当您更新模块时,您需要实现 hook_update_n(), ,该模块一直更新。同时,实施 hook_schema() 需要更新以包括模块中使用的所有架构,以便谁首次安装模块。

许可以下: CC-BY-SA归因
scroll top