1、现阶段的表设计(选题与资源的关联),字段:plan_id 表示记录属于哪一个选题ID,如图1

2、在编辑选题页面,上传了 2 个资源文件,如图2

3、保存选题之后,打开资源表,数据记录,如图3

4、之前设计表结构时,资源表仅用于选题下的资源文件,但是,现在在其他页面(基地设置、用户设置)中,也需要上传资源文件,如图4

5、因此,决定重命名表名,plan_asset 为 asset,删除字段:plan_id。在选题表 plan 中添加字段 素材的资源ID,多个用,号隔开:material_asset_id,重构之后,像步骤 3 中,资源表中新增了 2 条记录,主键ID分别为:13、14,则选题表 plan 中字段:material_asset_id 的值为:13,14,以此来确定选题与资源的关联关系。
6、选题表 plan 中添加字段:material_asset_id,迁移表 plan_asset 中的关联关系至 material_asset_id,遍历选题记录,在选题资源表中查询是否存在对应选题的资源记录,如果存在,则将资源主键ID更新至 material_asset_id,迁移后的结果,符合预期,如图5

PS E:\wwwroot\pcs-api> ./yii migrate
Yii Migration Tool (based on Yii v2.0.15.1)
Total 1 new migration to be applied:
m191016_022618_add_material_asset_id_to_plan
Apply the above migration? (yes|no) [no]:yes
*** applying m191016_022618_add_material_asset_id_to_plan
> add column material_asset_id string(1024) NOT NULL DEFAULT '' COMMENT '素材的资源ID,多个用,号隔开' AFTER `opinion
` to table {{%plan}} ... done (time: 0.081s)
> update {{%plan}} ... done (time: 0.005s)
> update {{%plan}} ... done (time: 0.002s)
*** applied m191016_022618_add_material_asset_id_to_plan (time: 0.112s)
1 migration was applied.
Migrated up successfully.
2 13
3 14,15,16,17,18
7、选题与资源的关联关系迁移完毕后,则重命名表名,plan_asset 为 asset,删除字段:plan_id。后续 asset 表做为所有数据的资源文件的公共表,关联关系在各自的数据表中自行维护。如图6

PS E:\wwwroot\pcs-api> ./yii migrate
Yii Migration Tool (based on Yii v2.0.15.1)
Total 1 new migration to be applied:
m191016_070338_rename_plan_asset_to_asset
Apply the above migration? (yes|no) [no]:yes
*** applying m191016_070338_rename_plan_asset_to_asset
> rename table {{%plan_asset}} to {{%asset}} ... done (time: 0.023s)
> drop comment from table {{%asset}} ... done (time: 0.011s)
> add comment on table {{%asset}} ... done (time: 0.009s)
> drop column plan_id from table {{%asset}} ... done (time: 0.064s)
*** applied m191016_070338_rename_plan_asset_to_asset (time: 0.120s)
1 migration was applied.
Migrated up successfully.
需要长期技术维护或远程问题排查?
我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。
如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:
- ✅ PHP / Laravel / Yii2 老项目无人维护
- ✅ Go / Gin 后端接口需要排查或优化
- ✅ WordPress 网站访问慢、报错或插件冲突
- ✅ Nginx / MySQL / Redis / Linux 服务器异常
- ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
- ✅ 需要长期远程技术支持或兼职维护
更多介绍请查看:关于我 & 合作
微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan
