Migration::changeField PHP Method

changeField() public method

Modify field for migration
public changeField ( $table, $oldfield, $newfield, $type, $options = [] )
$table string
$oldfield string old name of the field
$newfield string new name of the field
$type string (see fieldFormat)
$options array - default_value new field's default value, if a specific default value needs to be used - comment comment to be added during field creation - nodefault : do not define default value (default false)
    function changeField($table, $oldfield, $newfield, $type, $options = array())
    {
        $params['value'] = NULL;
        $params['nodefault'] = false;
        $params['comment'] = '';
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $params[$key] = $val;
            }
        }
        $format = $this->fieldFormat($type, $params['value'], $params['nodefault']);
        if ($params['comment']) {
            $params['comment'] = " COMMENT '" . addslashes($params['comment']) . "'";
        }
        if (FieldExists($table, $oldfield, false)) {
            // in order the function to be replayed
            // Drop new field if name changed
            if ($oldfield != $newfield && FieldExists($table, $newfield)) {
                $this->change[$table][] = "DROP `{$newfield}` ";
            }
            if ($format) {
                $this->change[$table][] = "CHANGE `{$oldfield}` `{$newfield}` {$format} " . $params['comment'] . "";
            }
            return true;
        }
        return false;
    }

Usage Example

 static function install(Migration $migration)
 {
     global $DB, $GENINVENTORYNUMBER_TYPES;
     $table = getTableForItemType(__CLASS__);
     if (TableExists("glpi_plugin_geninventorynumber_fields")) {
         //Only migrate itemtypes when it's only necessary, otherwise it breaks upgrade procedure !
         $migration->renameTable("glpi_plugin_geninventorynumber_fields", $table);
     }
     if (!TableExists($table)) {
         $query = "CREATE TABLE IF NOT EXISTS `{$table}` (\n            `id` int(11) NOT NULL auto_increment,\n            `plugin_geninventorynumber_configs_id` int(11) NOT NULL default '0',\n            `itemtype` varchar(255) COLLATE utf8_unicode_ci DEFAULT '',\n            `template` varchar(255) COLLATE utf8_unicode_ci DEFAULT '',\n            `is_active` tinyint(1) NOT NULL default '0',\n            `use_index` tinyint(1) NOT NULL default '0',\n            `index` bigint(20) NOT NULL default '0',\n            PRIMARY KEY  (`id`)\n            ) ENGINE=MyISAM  CHARSET=utf8 COLLATE=utf8_unicode_ci;";
         $DB->query($query);
     } else {
         $migration->changeField($table, 'ID', 'id', 'autoincrement');
         $migration->changeField($table, 'config_id', 'plugin_geninventorynumber_configs_id', 'integer');
         if ($migration->changeField($table, 'device_type', 'itemtype', 'string')) {
             $migration->migrationOneTable($table);
             Plugin::migrateItemType(array(), array("glpi_displaypreferences"), array($table));
         }
         $migration->changeField($table, 'enabled', 'is_active', 'boolean');
         $migration->changeField($table, 'use_index', 'use_index', 'boolean');
         $migration->migrationOneTable($table);
     }
     $field = new self();
     foreach ($GENINVENTORYNUMBER_TYPES as $type) {
         if (!countElementsInTable($table, "`itemtype`='{$type}'")) {
             $input["plugin_geninventorynumber_configs_id"] = 1;
             $input["itemtype"] = $type;
             $input["template"] = "<#######>";
             $input["is_active"] = 0;
             $input["index"] = 0;
             $field->add($input);
         }
     }
 }
All Usage Examples Of Migration::changeField