在advanced\console\migrations文件夾下有一個 m130524_201442_init.php 文件php
<?php use yii\db\Schema; use yii\db\Migration; class m130524_201442_init extends Migration { public function up() { $tableOptions = null; if ($this->db->driverName === 'mysql') { // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } $this->createTable('`user`', [ 'id' => $this->primaryKey(), 'username' => $this->string()->notNull()->unique(), 'auth_key' => $this->string(32)->notNull(), 'password_hash' => $this->string()->notNull(), 'password_reset_token' => $this->string()->unique(), 'email' => $this->string()->notNull()->unique(), 'status' => $this->smallInteger()->notNull()->defaultValue(10), 'created_at' => $this->integer()->notNull(), 'updated_at' => $this->integer()->notNull(), ], $tableOptions); } public function down() { $this->dropTable('`user`'); } }
用cmd命令行進入advanced目錄 ( 該目錄下有yii.bat )mysql
執行命令,選yes(輸入y)sql
這樣就在數據庫中新建了一張表user和另外一張表migration數據庫
其中migration表的內容大體以下,猜測這個version字段和每次執行命令時php文件的名字&文件裏的class名有關,因此每次執行命令時須要改動文件名和文件裏面的class名yii
那麼,如今新建一張blog表,包含id、title、content、create_time四個字段ide
先將該php文件複製備份, 再將該文件重命名爲m330524_201442_init.php(隨機數字,只要和已有的version不一樣)this
再將文件內的class m220524_201442_init extends Migration 中的 220524 改成 330524 (和文件名同樣)spa
編輯字段內命令行
<?php use yii\db\Schema; use yii\db\Migration; class m220524_201442_init extends Migration { public function up() { $tableOptions = null; if ($this->db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT="文章表"'; } $this->createTable('blog', [ 'id' => $this->primaryKey(), 'title' => $this->string(100)->notNull()->defaultValue(''), 'content' => $this->text(), 'create_time' => $this->datetime(), ],$tableOptions); } public function down() { $this->dropTable('blog'); } }
最後執行命令,選yes
3d
yii migrate console/migrations/m130524_201442_init.php
能夠看到blog表建好了.