`
羽落窗前
  • 浏览: 105917 次
  • 性别: Icon_minigender_2
  • 来自: 厦门
社区版块
存档分类
最新评论

Drupal模块自定义版本更新(hook_update_N)的用法

阅读更多

自己编写的Drupal模块,有时候需要对数据库表进行修改,但是又不想抹掉已经在数据库中存有数据的表格,就要用到hook_update_N()。

 

步骤:

1。在模块的 .install 文件夹下添加hook_update_N()的钩子. 按照drupal官网文档上的命名规则,建议用version版本号来定义N是多少。例如,如果.info里version = ‘6.x-1.2', 那么function名可以定义为 xxxx_update_6012()

 

2.  以下是几种修改数据库表的例子

 

- Adding a new column

 

<?php
function mymodule_update_6100() {
  $ret = array();
  db_add_field($ret, 'mytable1', 'newcol', array('type' => 'int', 'not null' => TRUE));
  return $ret;
}
?>
 

 

- Add a new table

 

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

 

 

- Add a new key

 

<?php
function mymodule_update_6102() {
  $ret = array();
  db_add_unique_key($ret, 'mytable2', 'mykey', array('field1', 'field2')); 
  return $ret;
}
?>
 

 

3. 添加完成之后,进入update.php,点击continue,在 Select version里就能看到了。

注意:Drupal 6 有一个bug, 如果module的名字里包含大写字母,在select version里会找不到这个模块的更新版本。解决方法是,在includes/install.inc文件里,添加一行代码,如下:

function drupal_get_schema_versions($module) {
  。。。
  $module = strtolower($module); //加上这一行
  foreach ($functions['user'] as $function) {
  。。。
}
 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics