你可以在迁移里通过`table()`方法获取到Table对象的实例 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $table = $this->table('tableName'); } /** * Migrate Down. */ public function down() { } } ~~~ ## 创建一个Table ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $users = $this->table('users'); $users->addColumn('username', 'string', array('limit' => 20)) ->addColumn('password', 'string', array('limit' => 40)) ->addColumn('password_salt', 'string', array('limit' => 40)) ->addColumn('email', 'string', array('limit' => 100)) ->addColumn('first_name', 'string', array('limit' => 30)) ->addColumn('last_name', 'string', array('limit' => 30)) ->addColumn('created', 'datetime') ->addColumn('updated', 'datetime', array('null' => true)) ->addIndex(array('username', 'email'), array('unique' => true)) ->save(); } /** * Migrate Down. */ public function down() { } } ~~~ > 工具会自动创建一个名为`id`的自增主键 如果你不想使用主键,或者自己自定义主键,可以下面这种方法 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $table = $this->table('followers', array('id' => false, 'primary_key' => array('user_id', 'follower_id'))); $table->addColumn('user_id', 'integer') ->addColumn('follower_id', 'integer') ->addColumn('created', 'datetime') ->save(); } /** * Migrate Down. */ public function down() { } } ~~~ 要设置一个不开启`AUTO_INCREMENT`的主键的话,可以通过覆盖默认主键名`id`来实现 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $table = $this->table('followers', array('id' => 'user_id')); $table->addColumn('user_id', 'integer') ->addColumn('follower_id', 'integer') ->addColumn('created', 'datetime', array('default' => 'CURRENT_TIMESTAMP')) ->save(); } /** * Migrate Down. */ public function down() { } } ~~~ ## 有效的字段类型 * string * text * integer * biginteger * float * decimal * datetime * timestamp * time * date * binary * boolean > Mysql 还支持 `enum `和 `set` 字段类型 ## 检查一个表是否已经存在 可以使用`hasTable()`方法 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $exists = $this->hasTable('users'); if ($exists) { // do something } } /** * Migrate Down. */ public function down() { } } ~~~ ## 删除一个表 可以使用`dropTable()`方法,但最好在down方法里重建一下这个表 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $this->dropTable('users'); } /** * Migrate Down. */ public function down() { $users = $this->table('users'); $users->addColumn('username', 'string', array('limit' => 20)) ->addColumn('password', 'string', array('limit' => 40)) ->addColumn('password_salt', 'string', array('limit' => 40)) ->addColumn('email', 'string', array('limit' => 100)) ->addColumn('first_name', 'string', array('limit' => 30)) ->addColumn('last_name', 'string', array('limit' => 30)) ->addColumn('created', 'datetime') ->addColumn('updated', 'datetime', array('null' => true)) ->addIndex(array('username', 'email'), array('unique' => true)) ->save(); } } ~~~ ## 重命名表名 可以使用`rename()`方法 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $table = $this->table('users'); $table->rename('legacy_users'); } /** * Migrate Down. */ public function down() { $table = $this->table('legacy_users'); $table->rename('users'); } } ~~~ ## 获取所有的字段 `getColumns()`方法 ~~~ <?php use Think\Migration\Migration; class ColumnListMigration extends Migration { /** * Migrate Up. */ public function up() { $columns = $this->table('users')->getColumns(); ... } /** * Migrate Down. */ public function down() { ... } } ~~~ ## 检查一个字段是否已经存在了 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Change Method. */ public function change() { $table = $this->table('user'); $column = $table->hasColumn('username'); if ($column) { ... } } } ~~~ ## 字段重命名 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $table = $this->table('users'); $table->renameColumn('bio', 'biography'); } /** * Migrate Down. */ public function down() { $table = $this->table('users'); $table->renameColumn('biography', 'bio'); } } ~~~ ## 添加字段到某个字段之后 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Change Method. */ public function change() { $table = $this->table('users'); $table->addColumn('city', 'string', array('after' => 'email')) ->update(); } } ~~~ ## 删除一个字段 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Change Method. */ public function change() { $table = $this->table('users'); $table->removeColumn('short_name') ->update(); } } ~~~ ## 限制字段长度 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Change Method. */ public function change() { $table = $this->table('tags'); $table->addColumn('short_name', 'string', array('limit' => 30)) ->update(); } } ~~~ ## 设置索引 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $table = $this->table('users'); $table->addColumn('city', 'string') ->addIndex(array('city')) ->save(); } /** * Migrate Down. */ public function down() { } } ~~~ > 默认的会创建 normal 索引,如果需要创建unique 索引可以使用下面这种写法 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $table = $this->table('users'); $table->addColumn('email', 'string') ->addIndex(array('email'), array('unique' => true)) ->save(); } /** * Migrate Down. */ public function down() { } } ~~~ ## 删除索引 ~~~ <?php use Think\Migration\Migration; class MyNewMigration extends Migration { /** * Migrate Up. */ public function up() { $table = $this->table('users'); $table->removeIndex(array('email')); } /** * Migrate Down. */ public function down() { } } ~~~