从演示博客 SQL 创建 yii 迁移


Creating yii migration from demo blog sql

我有一个来自yii演示博客的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"或"字符串")?是否可以添加文件迁移"插入","更新"?

在你的函数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