在advanced\console\migrations文件夾下有一個 m130524_201442_init.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 )
執行命令,選yes(輸入y)
這樣就在數據庫中新建了一張表user和另一張表migration
其中migration表的內容大致如下,猜想這個version字段和每次執行命令時php文件的名字&文件裏的class名有關,所以每次執行命令時需要改動文件名和文件裏面的class名
那麼,現在新建一張blog表,包含id、title、content、create_time四個字段
先將該php文件複製備份, 再將該文件重命名爲m330524_201442_init.php(隨機數字,只要和已有的version不同)
再將文件內的class m220524_201442_init extends Migration 中的 220524 改爲 330524 (和文件名一樣)
編輯字段內
<?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
yii migrate console/migrations/m130524_201442_init.php
可以看到blog表建好了.