开发者

Drupal: module update

I've module and I've update to alter database table, shortly I need to do something like

ALTER TABLE `TABLE` ADD `FIELD` INT UNSIGNED NOT 开发者_如何学CNULL AFTER `SOME_FIELD` 

so is there any built in function in Drupal to make this changes I considered db_add_field function didn't work?

Sultan


Using db_add_field()

db_add_field('TABLE', 'FIELD', "VARCHAR( 255 ) NOT NULL DEFAULT '0' AFTER FIELD_2");


The above does not work, for one it leaves out the first argument (the reference to $ret) and the fourth argument will not allow a raw sql query, only a structured array.

What I had to do was this (change hook_update_N to modulename_update_XXXX as per the drupal api documentation of course):

function hook_update_N(&$sandbox) {
  // We use update_sql here, instead of db_add_field because we cannot specify
  // AFTER in the db_add_field.
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {table} ADD `FIELD` INT UNSIGNED NOT NULL AFTER `SOME_FIELD`");
  return $ret;
}

Hope this helps someone else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜