# 數據庫migration的使用改造-表
用到的類庫
```php
use ClassLibrary\ClMigrateTable;
```
我用一個用戶表來做例子。先創建數據庫表
```php
php think migrate:create User
```
這個時候對應創建了
```php
database/migrations/20180122074850_user.php
```
生成的文檔內容
```php
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class User extends Migrator
{
public function change()
{
}
}
```
修改代碼之后
```php
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use ClassLibrary\ClMigrateTable;
class User extends Migrator
{
public function up()
{
$table = 'user';
if($this->hasTable($table)){
return;
}
$this->table($table)
->setComment(
ClMigrateTable::instance()
->fetch('用戶表')
)
->create();
}
public function down()
{
parent::down();
$table = 'user';
if($this->hasTable($table)){
$this->dropTable($table);
}
}
}
```
此時,看下數據庫表DDL
```sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='{"name":"用戶表","create_api":["getList","get","create","delete","update"]}';
```
其中COMMENT,是一個JSON字符串,這個JSON的字符串的想象空間就無窮了。
### ClMigrateTable函數說明
函數名 | 注釋
---|---
instance | 獲取實例對象
fetch | 獲取表Comment定義Json
usingCache | 是否使用緩存,如果緩存時間大于0,則會生成帶緩存的代碼1
createView | 暫時忽略該方法,為后期考慮
createApi | 用于控制創建的方法,默認會自動創建5個Api接口
getUpdateCommentSql | 用于獲取修改table.comment的注釋語句,目前Migrate沒有提供修改表comment的方法,故采用直接執行sql語句的辦法來修改表comment