Last active
December 10, 2015 22:08
-
-
Save jamband/4500270 to your computer and use it in GitHub Desktop.
Yii Framework: Creating Migrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array( | |
... | |
'commandMap' => array( | |
'migrate' => array( | |
'class' => 'system.cli.commands.MigrateCommand', | |
'migrationTable' => 'migration', | |
'templateFile' => 'application.migrations.template', | |
), | |
), | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class m130110_074533_init extends CDbMigration | |
{ | |
public function safeUp() | |
{ | |
$options = ''; | |
if (Yii::app()->db->schema instanceof CMysqlSchema) { | |
$options = 'ENGINE=InnoDB DEFAULT CHARSET=utf8'; | |
} | |
// user | |
$this->createTable('user', array( | |
'id' => 'pk', | |
'username' => 'string NOT NULL', | |
'email' => 'string NOT NULL', | |
'password' => 'string NOT NULL', | |
), $options); | |
$this->createIndex('user_username', 'user', 'username', true); | |
$this->createIndex('user_email', 'user', 'email', true); | |
//word | |
$this->createTable('word', array( | |
'id' => 'pk', | |
'userid' => 'integer NOT NULL', | |
'en' => 'string NOT NULL', | |
'ja' => 'string NOT NULL', | |
), $options); | |
$this->addForeignKey('fk_word_userid', 'word', 'userid', 'user', 'id', 'CASCADE', 'CASCADE'); | |
$this->createIndex('word_en_userid', 'word', 'en, userid', false); | |
} | |
public function safeDown() | |
{ | |
$this->dropTable('word'); | |
$this->dropTable('user'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class {ClassName} extends CDbMigration | |
{ | |
public function safeUp() | |
{ | |
$options = ''; | |
if (Yii::app()->db->schema instanceof CMysqlSchema) { | |
$options = 'ENGINE=InnoDB DEFAULT CHARSET=utf8'; | |
} | |
} | |
public function safeDown() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment