Yiiでのマイグレーションの方法については、公式ガイドが詳しいので、基本的な使い方については、このページをみてください。
マイグレーションで利用できるカラムの型については、こちらのページにあるように
- pk: an auto-incremental primary key type, will be converted into “int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY”
- string: string type, will be converted into “varchar(255)”
- text: a long string type, will be converted into “text”
- integer: integer type, will be converted into “int(11)”
- boolean: boolean type, will be converted into “tinyint(1)”
- float: float number type, will be converted into “float”
- decimal: decimal number type, will be converted into “decimal”
- datetime: datetime type, will be converted into “datetime”
- timestamp: timestamp type, will be converted into “timestamp”
- time: time type, will be converted into “time”
- date: date type, will be converted into “date”
- binary: binary data type, will be converted into “blob”
となっています。これ以外の型を使いたい場合、
'name'=>'VARCHAR(128) not null',
のような感じに指定するとよいです。(普通にSQLが利用できる)
その他の情報として、デフォルトの型や文字コードを設定するには、
class m121114_062440_create_lookup_table extends CDbMigration { protected $options = 'ENGINE=InnoDB DEFAULT CHARSET=utf8'; public function up() { $this->createTable('tbl_lookup',array( 'id'=>'pk', 'name'=>'VARCHAR(128) not null', 'code'=>'integer not null', 'type'=>'VARCHAR(128) not null', 'position'=>'integer not null', ), $this->options); } }
のようにすればよいです。
また、indexを利用するには、
$this->createTable();
の後に
$this->createIndex('tbl_lookup_type', 'tbl_lookup', 'type', false);
のような行を追加すればよいです。
createIndex関数の引数は、index名、対象テーブル名、対象フィールド名、ユニークにするかどうかです。(詳細はこちら)
あと、外部キーを利用する場合、
$this->addForeignKey('FK_category', 'tbl_event_category', 'category_id', 'tbl_category', 'id', 'CASCADE', 'CASCADE');
のような行を追加すればよいです。
addForeignKey関数の引数は、外部キー名、対象となるテーブル名、対象となるフィールド名、外部キーが参照しているテーブル名、外部キーが参照しているフィールド名、ON DELETEの際のオプション、ON UPDATEの際のオプションです。(詳細はこちら)
1件のコメント
コメントは受け付けていません。