网站建设 流程,视频网站 怎么做,怎么搜 织梦的网站,网站设计任务书安装
composer require topthink/think-migration
创建迁移工具文件
//执行命令,创建一个操作文件,一定要用大驼峰写法,如下
php think migrate:create AnyClassNameYouWant
//执行完成后,会在项目根目录多一个database目录,这里面存放类库操作文件
//文件名类似/database/m…安装
composer require topthink/think-migration
创建迁移工具文件
//执行命令,创建一个操作文件,一定要用大驼峰写法,如下
php think migrate:create AnyClassNameYouWant
//执行完成后,会在项目根目录多一个database目录,这里面存放类库操作文件
//文件名类似/database/migrations/20190615151716_any_class_name_you_want.php
编辑文件
?phpuse think\migration\Migrator;
use think\migration\db\Column;class AnyClassNameYouWant extends Migrator
{/*** Change Method.** Write your reversible migrations using this method.** More information on writing migrations is available here:* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class** The following commands can be used in this method and Phinx will* automatically reverse them when rolling back:** createTable* renameTable* addColumn* renameColumn* addIndex* addForeignKey** Remember to call create() or update() and NOT save() when working* with the Table class.*/public function change(){// create the table$table $this-table(users,array(engineMyISAM));$table-addColumn(username, string,array(limit 15,default,comment用户名登陆使用))-addColumn(password, string,array(limit 32,defaultmd5(123456),comment用户密码)) -addColumn(login_status, boolean,array(limit 1,default0,comment登陆状态))-addColumn(login_code, string,array(limit 32,default0,comment排他性登陆标识))-addColumn(last_login_ip, integer,array(limit 11,default0,comment最后登录IP))-addColumn(last_login_time, datetime,array(default0,comment最后登录时间))-addColumn(is_delete, boolean,array(limit 1,default0,comment删除状态1已删除))-addIndex(array(username), array(unique true))-create();}}
执行迁移工具
php think migrate:run 表支持的参数选项
选项描述comment给表结构设置文本注释row_format设置行记录模格式engine表引擎 (默认 InnoDB)collation表字符集 (默认 utf8\_general\_ci)signed是否无符号 signed(默认 true)
常用列
bigintegerbinarybooleandatedatetimedecimalfloatintegerstringtexttimetimestampuuid
所有的类型都支持的参数
OptionDescriptionlimit文本或者整型的长度lengthlimit别名default默认值null允许 NULL 值 (不该给主键设置after在哪个字段名后 (只对MySQL有效)comment给列设置文本注释
索引的用法 -addIndex([email,username], [limit [email 5, username 2]])-addIndex(user_guid, [limit 6])-addIndex(email,[typefulltext])如上面例子所示默认是普通索引mysql可设置生效复合索引mysql可以设置fulltext.
自动版本升级降级
该项目可以升级和还原就像git/svn一样rollback。
如果希望实现自动升级降级那就把逻辑写在change方法里只最终调用create和update方法不要调用save方法。
change方法内仅支持以下操作
createTablerenameTableaddColumnrenameColumnaddIndexaddForeignKey
如果真的有调用其他方法可以写到up和down方法里这里的逻辑不支持自动还原up写升级的逻辑down写降级的逻辑。 public function change(){// create the table$table $this-table(user_logins);$table-addColumn(user_id, integer)-addColumn(created, datetime)-create();}/*** Migrate Up.*/public function up(){}/*** Migrate Down.*/public function down(){}