Yii 中的数据库迁移

5
为了在Yii中进行迁移,我使用了以下代码:CDbSchema::getColumnType
<?php

class m110714_122129_users extends CDbMigration
{
  public function up()
  {
        $this-> createTable('{{users}}',array(
   'id' => 'pk',
   'username' => 'VARCHAR (80) NOT NULL',
   'password' => 'VARCHAR (80) NOT NULL',
   'email' => 'VARCHAR (128) NOT NULL',
   'activekey' => 'VARCHAR (128) NOT NULL DEFAULT \'\'',
   'createtime' => 'INTEGER (10) NOT NULL DEFAULT \'0\' ',
   'lastvisit' => 'INTEGER (10) NOT NULL DEFAULT \'0\' ',
   'superuser' => 'INTEGER (1) NOT NULL DEFAULT \'0\' ',
    'status'=> 'INTEGER (1) NOT NULL DEFAULT \'0\' ',
));

  }

  public function down()
  {
    echo "m110714_122129_users does not support migration down.\n";
    return false;
  }

  /*
  // Use safeUp/safeDown to do migration with transaction
  public function safeUp()
  {
  }

  public function safeDown()
  {
  }
  */
}

为了获取

 CREATE TABLE IF NOT EXISTS `nt_users` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(20) NOT NULL,
      `password` varchar(128) NOT NULL,
      `email` varchar(128) NOT NULL,
      `activkey` varchar(128) NOT NULL DEFAULT '',
      `createtime` int(10) NOT NULL DEFAULT '0',
      `lastvisit` int(10) NOT NULL DEFAULT '0',
      `superuser` int(1) NOT NULL DEFAULT '0',
      `status` int(1) NOT NULL DEFAULT '0',
    );

但是现在我想做一些更改,比如做唯一键。

UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`),
  KEY `status` (`status`),
  KEY `superuser` (`superuser`)

那么如何制作呢?我查阅了Yii的文档,但没有找到相关信息。希望能得到帮助,不胜感激。
2个回答

13

只需将它们作为非关联值提供在数组中,像这样:

$this-> createTable('{{users}}',array(
   'id' => 'pk',
   'username' => 'VARCHAR (80) NOT NULL',
   'password' => 'VARCHAR (80) NOT NULL',
   'email' => 'VARCHAR (128) NOT NULL',
   'activekey' => 'VARCHAR (128) NOT NULL DEFAULT \'\'',
   'createtime' => 'INTEGER (10) NOT NULL DEFAULT \'0\' ',
   'lastvisit' => 'INTEGER (10) NOT NULL DEFAULT \'0\' ',
   'superuser' => 'INTEGER (1) NOT NULL DEFAULT \'0\' ',
   'status'=> 'INTEGER (1) NOT NULL DEFAULT \'0\' ',
   'UNIQUE KEY `username` (`username`)',
   'UNIQUE KEY `email` (`email`)',
   'KEY `status` (`status`)',
   'KEY `superuser` (`superuser`)',
));

这样它们将被添加到创建语句的末尾,而不做任何更改。


12
$this->createTable('{{users}}',
    array(
        'id' => 'pk',
        'username' => 'varchar(80) NOT NULL',
        'password' => 'varchar(80) NOT NULL',
        'email' => 'varchar(128) NOT NULL',
        'activkey' => 'varchar(128) NOT NULL DEFAULT \'\'',
        'createtime' => 'integer(10) NOT NULL DEFAULT \'0\'',
        'lastvisit' => 'integer(10) NOT NULL DEFAULT \'0\'',
        'superuser' => 'integer(1) NOT NULL DEFAULT \'0\'',
        'status' => 'integer(1) NOT NULL DEFAULT \'0\'',
    ),
);

$this->createIndex('username', '{{user}}', 'username', true);
$this->createIndex('email', '{{user}}', 'email', true);
$this->createIndex('superuser', '{{user}}', 'superuser', false);
$this->createIndex('status', '{{user}}', 'status', false);

请查看http://www.yiiframework.com/doc/api/1.1/CDbMigration#createIndex-detail获取更多详细信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接