5

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..

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.