Yii中的数据库迁移

时间:2011-07-14 12:30:02

标签: php mysql yii database-migration

为了做migration in Yii,我使用了这一行

<?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 个答案:

答案 0 :(得分: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`)',
));

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

答案 1 :(得分:11)

$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