I have a SQL code from yii demo blog:
CREATE TABLE tbl_post
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(128) NOT NULL,
content TEXT NOT NULL,
tags TEXT,
status INTEGER NOT NULL,
create_time INTEGER,
update_time INTEGER,
author_id INTEGER NOT NULL,
CONSTRAINT FK_post_author FOREIGN KEY (author_id)
REFERENCES tbl_user (id) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO tbl_lookup (name, type, code, position) VALUES ('Draft', 'PostStatus', 1, 1);
I need to write it to a file migration in a function up(), how to write the code? And where can I read about the addition of tables in the migration file (I mean how to write, for example "varchar" or "string")? Is it possible to add a file migration "INSERT", "UPDATE"?
In your function up() or safeUp() you would add the following code:
$this->createTable('tbl_post', array(
"id" => "pk",
"title" => "VARCHAR(128) NOT NULL",
"content" => "TEXT NOT NULL",
"tags" => "TEXT",
"status" => "INT NOT NULL",
"create_time" => "INT",
"update_time" => "INT",
"author_id" => "INT NOT NULL",
), "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
$this->addForeignKey('FK_post_author', 'tbl_post', 'author_id', 'tbl_user', 'id', 'CASCADE', 'RESTRICT');
$this->insert('tbl_lookup', array(
"name" => "Draft",
"type" => "PostStatus",
"code" => 1,
"position" => 1,
);
There is an update() method available as well (insert has been shown above): http://www.yiiframework.com/doc/api/1.1/CDbMigration#update-detail