0

我有一个来自 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”或“string”)?是否可以添加文件迁移“INSERT”、“UPDATE”?

4

1 回答 1

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

于 2013-09-30T20:54:35.930 回答