简体   繁体   English

如何处理多个多对多关系

[英]How to handle multiple many to many relationships

I have a Laravel project in which there is likely to be multiple many to many relationships but I feel like I'm going around in circles in terms of the correct use of pivots tables.我有一个 Laravel 项目,其中可能存在多个多对多关系,但我觉得在正确使用数据透视表方面我正在绕圈子。

The entities are as follows:实体如下:

  • Centre: an office that is going to open soon and has information such as name, location, postcode, opening date中心:即将开业的办公室,有名称、位置、邮政编码、开业日期等信息
  • Task: a task that must be completed before a centre can open, it has information such as name, department and centre type任务:中心开通前必须完成的任务,包含名称、部门、中心类型等信息
  • Department: a department represents a key area such as IT, Marketing, Sales etc. Each task would have a reference to a department.部门:一个部门代表一个关键领域,例如 IT、市场营销、销售等。每个任务都会引用一个部门。
  • User: a user is a user of the system, they belong to a department and they can complete tasks.用户:用户是系统的用户,属于一个部门,可以完成任务。

Considerations:注意事项:

  • A department has users, so users would have a department_id一个部门有用户,所以用户会有一个 department_id
  • A task has a department so would also have a department_id一个任务有一个部门,所以也会有一个 department_id
  • A centre has departments so a pivot called something like centre_department with centre_id and department_id一个中心有部门,所以一个 pivot 叫做 center_department,有 center_id 和 department_id
  • A user has tasks assigned to them but only tasks that belong to a centre用户有任务分配给他们,但只有属于中心的任务

The bottom statement is where I'm going in circles because:底部的陈述是我要绕圈子的地方,因为:

  • A Task is to be assigned to a Centre which is fine as you could make a table like centre_task with centre_id and task id将任务分配给一个中心,这很好,因为您可以使用 center_id 和任务 id 制作像 center_task 这样的表
  • A user has tasks assigned to them, however, is it okay to have a task_user table that has user_id and a centre_task_id, effectively a pivot table that uses another pivot table?用户有任务分配给他们,但是,是否可以有一个具有 user_id 和 center_task_id 的 task_user 表,实际上是一个使用另一个 pivot 表的 pivot 表?

This is because a user can't be directly assigned to a task, they can only be assigned to a task within a centre as the tasks have a many to many relationship with centres.这是因为用户不能直接分配给任务,他们只能分配给中心内的任务,因为任务与中心具有多对多关系。

If I were to assign a user just using task id, they would be assigned to that task in every related centre.如果我只使用任务 ID 分配一个用户,他们将被分配到每个相关中心的该任务。

So, again is it okay to have a pivot table that uses another pivot table, or does this display an issue with the structure I have suggested?那么,再次拥有一个使用另一个 pivot 表的 pivot 表是否可以,或者这是否显示了我建议的结构的问题?

Intended flow预期流量

  • A user creates a task, assigns it a department and a type so that this task is only relevant to a specific department and type of centre.用户创建任务,为其分配部门和类型,以便该任务仅与特定部门和中心类型相关。
  • A user can assign a task within a centre to a user who is in the department that's relevant to the department of the task用户可以将中心内的任务分配给与任务部门相关的部门中的用户
  • The task in the centre would then have an assignee and a deadline然后,中心的任务将有一个受让人和一个截止日期

In theory I'd end up with a pivot table that has the following:理论上,我最终会得到一个 pivot 表,该表具有以下内容:

  • user_id用户身份
  • centre_task_id center_task_id
  • deadline最后期限
  • date_completed date_completed

Essentially I just feel like I'm connecting too many pieces?本质上我只是觉得我连接了太多的部分?

Laravel does the linking for you, simply use (in this case) morphToMany() & morphedByMany() in your Models to establish a relation between two tables. Laravel 为您进行链接,只需在您的模型中使用(在这种情况下) morphToMany()morphedByMany()来建立两个表之间的关系。 There is a very good Laracast and also a very good Documentation.有一个非常好的 Laracast 和一个非常好的文档。 I have linked this below:我在下面链接了这个:

Laracast Video very nice Explaination for Many-To-Many Relations Laracast 视频很好地解释了多对多关系

Laravel Documentation for Eloquent ORM In General Laravel Eloquent ORM 的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM