yii2 migrate 数据库迁移的简单分享 [ 2.0 版本 ]

本文总结了在Yii框架中进行数据库操作的主要方法,包括创建表、修改表名、删除表、新增和修改字段、创建索引、添加及删除字段备注以及对表添加和删除备注。这些方法对于日常开发维护数据库至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发中经常会用到的方法小结:
1、./yii migrate/create 文件名

创建表:

$this->createTable(‘mtg_user’, [
‘id’ => $this->primaryKey()->comment(‘用户ID’),
‘project_id’ => $this->integer(11)->notNull()->comment(‘项目’),
‘username’ => $this->string(50)->notNull()->comment(‘登录账号’),
‘password’ => $this->string(50)->notNull()->comment(‘登录密码’),
‘role_id’ => $this->integer(11)->null()->comment(‘角色ID’),
‘realname’ => $this->string(50)->notNull()->comment(‘姓名’),
‘mobile’ => $this->string(11)->comment(‘手机号’),
‘bank_card’ => $this->string(50)->comment(‘银行卡号’),
‘email’ => $this->string(50)->comment(‘邮箱’),
‘idsn’ => $this->string(50)->comment(‘身份证号’),
‘wechat_user_id’ => $this->integer(11)->comment(‘微信用户ID’),
‘admin_id’ => $this->integer(11)->comment(‘添加人ID’),
‘price’ => $this->decimal(10,2)->notNull()->comment(‘费用’),
‘is_enabled’ => $this->tinyInteger(1)->defaultValue(1)->comment(‘是否启用’),
‘deleted’ => $this->tinyInteger(1)->defaultValue(0)->comment(‘是否删除’),
‘created_at’ => $this->dateTime()->comment(‘创建时间’),
‘updated_at’ => $this->dateTime()->comment(‘修改时间’),
‘content’ => $this->text()->null()->comment(‘协议内容’),
], ‘comment “会议用户”’);

修改表名

$this->renameTable(‘study_rename_table’,‘study_rename_table1’);

删除表

$this->dropTable(‘study_rename_table1’);

新增字段

$this−>addColumn(′user′,′testcolumn′,this->addColumn('user','test_column',this>addColumn(user,testcolumn,this->text()->comment(‘测试字段’)->after(‘is_bank_card_belong’));

修改字段

$this−>alterColumn(′user′,′testcolumn′,this->alterColumn('user','test_column',this>alterColumn(user,testcolumn,this->string(255)->comment(‘test_edit_column’));

修改字段属性

$this->renameColumn(‘mtg_meeting_docent’,‘service_charge_id’,‘docent_position_cost_id’);

修改字段名

$this->renameColumn(‘user’,‘test_column’,‘test_edit_column’);

删除字段

$this->dropColumn(‘user’,‘test_edit_column’);

创建索引

$this->createIndex(‘mobile_index’,‘user’,‘mobile’,True);

删除索引

$this->dropIndex(‘mobile_index’,‘user’);

给字段添加备注

$this->addCommentOnColumn(‘website_user’,‘retry_times’,‘重试次数’);

字段删除备注

$this->dropCommentFromColumn(‘website_user’,‘retry_times’);

给表添加备注

$this->addCommentOnTable(‘user_making_attach’,‘用户基本材料存储表’);

表删除备注

$this->dropCommentFromTable(‘user_making_attach’);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱好者777

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值