Skip to content

Instantly share code, notes, and snippets.

@jamband
Last active December 10, 2015 22:08
Show Gist options
  • Save jamband/4500270 to your computer and use it in GitHub Desktop.
Save jamband/4500270 to your computer and use it in GitHub Desktop.
Yii Framework: Creating Migrations
<?php
return array(
...
'commandMap' => array(
'migrate' => array(
'class' => 'system.cli.commands.MigrateCommand',
'migrationTable' => 'migration',
'templateFile' => 'application.migrations.template',
),
),
...
<?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');
}
}
<?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