Migration::addField PHP Method

addField() public method

Add a new GLPI normalized field
public addField ( $table, $field, $type, $options = [] )
$table string
$field string to add
$type string (see fieldFormat)
$options array - update : if not empty = value of $field (must be protected) - condition : if needed - value : default_value new field's default value, if a specific default value needs to be used - nodefault : do not define default value (default false) - comment : comment to be added during field creation - after : where adding the new field
    function addField($table, $field, $type, $options = array())
    {
        global $DB;
        $params['update'] = '';
        $params['condition'] = '';
        $params['value'] = NULL;
        $params['nodefault'] = false;
        $params['comment'] = '';
        $params['after'] = '';
        $params['first'] = '';
        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 ($params['after']) {
            $params['after'] = " AFTER `" . $params['after'] . "`";
        } else {
            if (isset($params['first'])) {
                $params['first'] = " FIRST ";
            }
        }
        if ($format) {
            if (!FieldExists($table, $field, false)) {
                $this->change[$table][] = "ADD `{$field}` {$format} " . $params['comment'] . " " . $params['first'] . $params['after'] . "";
                if (isset($params['update']) && strlen($params['update'])) {
                    $this->migrationOneTable($table);
                    $query = "UPDATE `{$table}`\n                         SET `{$field}` = " . $params['update'] . " " . $params['condition'] . "";
                    $DB->queryOrDie($query, $this->version . " set {$field} in {$table}");
                }
                return true;
            }
            return false;
        }
    }

Usage Example

Esempio n. 1
0
 static function install(Migration $migration)
 {
     global $DB;
     $obj = new self();
     $table = $obj->getTable();
     if (!TableExists($table)) {
         $migration->displayMessage("Installing {$table}");
         $query = "CREATE TABLE IF NOT EXISTS `{$table}` (\n                  `id`                                INT(11)        NOT NULL auto_increment,\n                  `name`                              VARCHAR(255)   DEFAULT NULL,\n                  `label`                             VARCHAR(255)   DEFAULT NULL,\n                  `type`                              VARCHAR(25)    DEFAULT NULL,\n                  `plugin_fields_containers_id`       INT(11)        NOT NULL DEFAULT '0',\n                  `ranking`                           INT(11)        NOT NULL DEFAULT '0',\n                  `default_value`                     VARCHAR(255)   DEFAULT NULL,\n                  `is_active`                         TINYINT(1)     NOT NULL DEFAULT '1',\n                  `is_readonly`                       TINYINT(1)     NOT NULL DEFAULT '1',\n                  `mandatory`                         TINYINT(1)     NOT NULL DEFAULT '0',\n                  PRIMARY KEY                         (`id`),\n                  KEY `plugin_fields_containers_id`   (`plugin_fields_containers_id`),\n                  KEY `is_active`                     (`is_active`),\n                  KEY `is_readonly`                   (`is_readonly`)\n               ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
         $DB->query($query) or die($DB->error());
     }
     $migration->displayMessage("Updating {$table}");
     if (!FieldExists($table, 'is_active')) {
         $migration->addField($table, 'is_active', 'bool', array('value' => 1));
         $migration->addKey($table, 'is_active', 'is_active');
     }
     if (!FieldExists($table, 'is_readonly')) {
         $migration->addField($table, 'is_readonly', 'bool', array('default' => false));
         $migration->addKey($table, 'is_readonly', 'is_readonly');
     }
     if (!FieldExists($table, 'mandatory')) {
         $migration->addField($table, 'mandatory', 'bool', array('value' => 0));
     }
     $migration->executeMigration();
     return true;
 }
All Usage Examples Of Migration::addField