dsjk3214 2011-07-14 12:30
浏览 18
已采纳

Yii中的数据库迁移

In order to doing migration in Yii I used this lines

<?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.
";
    return false;
  }

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

  public function safeDown()
  {
  }
  */
}

To get

 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',
    );

But now I want to make some changes like doing unique key

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

so how to make that? I searched Yii documentation but not found any thing. So any help will be highly appreciated..

  • 写回答

2条回答 默认 最新

  • douyinghuo8874 2011-07-14 16:06
    关注

    Just supply them as non-associative values in the array like this:

    $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`)',
    ));
    

    This way they will be added to the end of the create statement without any change.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?