上传资源文件的表结构的设计与思考,整体流程的重构

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

图1

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

图2

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

图3

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

图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

图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.
永夜