Yii 中的数据库迁移

发布于 2024-11-23 15:36:31 字数 1719 浏览 6 评论 0原文

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

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.\n";
    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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2024-11-30 15:36:31

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

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

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.

自找没趣 2024-11-30 15:36:31
$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 了解更多详情。

$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);

See http://www.yiiframework.com/doc/api/1.1/CDbMigration#createIndex-detail for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文