首页 > 解决方案 > 如何制作带有目录(字典)的通用表?

问题描述

我不断地完成这个任务。

我想找到一个一站式的解决方案。

我有几张桌子。它们有一组相同的字段。

表类型_应用程序

id   name   title         
1    game   Games         
2    app    Application   
3    arcade Arcade           

表 type_tasks

id   name      title          
1    create    Create App     
2    update    Update App     
3    publish   Publish App      

表 status_applications

id   name       title         
1    created    Games         
2    moderation Moderated application
3    publish    Application published

这些目录属于一个表。

表任务

id   name         title           status_id  type_task_id type_application_id  
1    first_app    My First game   3          2            1
2    second_app   My Second game  2          3            2
3    third_app    My third game   1          1            3

因此,有 4 个模型。

任务模型与应用程序类型模型、状态模型和任务类型模型有关系。

public function type_application()
{
    return $this->belongsTo(TypeApplication::class);
}

public function type_task()
{
    return $this->belongsTo(TypeTask::class);
}

public function status()
{
    return $this->belongsTo(Status::class);
}

如何使用目录(type_applications、type_tasks、status_applications)制作一个通用表?

我看到了制作表格类型的解决方案。

表类型

id   name       title                  type         
1    game       Games                  type_application
2    app        Application            type_application
3    arcade     Arcade                 type_application
4    create     Create App             type_task
5    update     Update App             type_task
6    publish    Publish App            type_task
7    created    Created application    status
8    moderation Moderated application  status
9    publish    Application published  status

任务模型中将存在与类型的关系。

public function type_application()
{
    return $this->belongsTo(Type::class)->whereType('type_application');
}

public function type_task()
{
    return $this->belongsTo(Type::class)->whereType('type_task');
}

public function status()
{
    return $this->belongsTo(Type::class)->whereType('status');
}

你能为这项任务提供更多解决方案吗?

也许某些设计模式适合这项任务?

标签: phplaravel

解决方案


推荐阅读