【问题标题】:database migration in YiiYii 中的数据库迁移
【发布时间】:2011-10-05 07:23:11
【问题描述】:

为了做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 文档,但没有找到任何东西。因此,任何帮助将不胜感激..

【问题讨论】:

    标签: php mysql yii database-migration


    【解决方案1】:
    $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

    【讨论】:

      【解决方案2】:

      只需将它们作为数组中的非关联值提供,如下所示:

      $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 语句的末尾而不做任何更改。

      【讨论】:

        猜你喜欢
        • 2011-12-10
        • 2014-10-07
        • 1970-01-01
        • 2014-10-18
        • 2011-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        相关资源
        最近更新 更多