我有来自yii demo blog的SQL代码:
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);
我需要将它写入函数up()中的文件迁移,如何编写代码? 我在哪里可以阅读迁移文件中添加表格(我的意思是如何编写,例如“varchar”或“string”)? 是否可以添加文件迁移“INSERT”,“UPDATE”?
答案 0 :(得分:4)
在你的函数up()或safeUp()中,你需要添加以下代码:
$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,
);
还有一个update()方法可用(插入已在上面显示): http://www.yiiframework.com/doc/api/1.1/CDbMigration#update-detail