Migration::addKey PHP Method

addKey() public method

Add index for migration
public addKey ( $table, $fields, $indexname = '', $type = 'INDEX', $len )
$table string
$fields string or array
$indexname string if empty =$fields (default '')
$type string index or unique (default 'INDEX')
$len integer for field length (default 0)
    function addKey($table, $fields, $indexname = '', $type = 'INDEX', $len = 0)
    {
        // si pas de nom d'index, on prend celui du ou des champs
        if (!$indexname) {
            if (is_array($fields)) {
                $indexname = implode($fields, "_");
            } else {
                $indexname = $fields;
            }
        }
        if (!isIndex($table, $indexname)) {
            if (is_array($fields)) {
                if ($len) {
                    $fields = "`" . implode($fields, "`({$len}), `") . "`({$len})";
                } else {
                    $fields = "`" . implode($fields, "`, `") . "`";
                }
            } else {
                if ($len) {
                    $fields = "`{$fields}`({$len})";
                } else {
                    $fields = "`{$fields}`";
                }
            }
            $this->change[$table][] = "ADD {$type} `{$indexname}` ({$fields})";
        }
    }

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::addKey