Skip to content

Instantly share code, notes, and snippets.

@keopx
Last active August 29, 2015 14:10
Show Gist options
  • Save keopx/c8a32cf1da8ad96c1822 to your computer and use it in GitHub Desktop.
Save keopx/c8a32cf1da8ad96c1822 to your computer and use it in GitHub Desktop.
Adding multiple new fields to a table
<?php
// @see: https://www.drupal.org/node/150215
/**
* Add new fields to 'mytable' table.
*/
function MYMODULE_update_7001() {
$fields = array(
'field_name_1' => array(
'description' => 'Field name 1',
'type' => 'varchar',
'length' => '32',
'not null' => FALSE,
'default' => '',
),
'field_name_2'=> array(
'description' => 'Field name 2',
'type' => 'varchar',
'length' => '32',
'not null' => FALSE,
'default' => '',
),
'field_name_3' => array(
'description' => 'Field name 3',
'type' => 'varchar',
'length' => '128',
'not null' => FALSE,
'default' => '',
),
);
foreach ($fields as $key => $field) {
if (!db_field_exists('mytable', $key)) {
db_add_field('mytable', $key, $field);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment