简体   繁体   English

我应该创建引用字段还是第三种内容类型来表示Drupal 8中的多对多关系?

[英]Should I create a reference field or a third content type to represent a many-to-many relationship in Drupal 8?

I have a student table and a course table that have a many-to-many relationship (a student can take many courses, and a course can be taken by many students). 我有一个学生表和一个课程表,它们之间具有多对多的关系(一个学生可以参加许多课程,而一个课程可以被许多学生参加)。

If I am implementing the above data model as a database, I would create a third table to represent the many-to-many relationship. 如果我将上述数据模型实现为数据库,则将创建第三个表来表示多对多关系。

But I want to implement the above data model in Drupal 8. I think that in Drupal 8 there are two ways to implement the above data model: 但是我想在Drupal 8中实现上述数据模型。我认为在Drupal 8中有两种方法可以实现上述数据模型:

  • I can create a reference field in one of the two content types (student or course) that points to the other content type. 我可以在两种内容类型(学生或课程)之一中创建一个指向另一种内容类型的参考字段。
  • I can create a third content type that have two reference fields that points to the student and course content types. 我可以创建第三个内容类型,它具有两个指向学生和课程内容类型的参考字段。

Am I correct that these two ways are valid? 我是否正确认为这两种方法有效? and if I am correct, which one should I choose? 如果我是正确的,我应该选择哪一个?

I think you're right, I would have suggested both ways. 我认为您是对的,我会建议两种方式。

As long as their connection doesn't have any additional parameters (eg date of subscription etc.), I would choose the entity reference within the students' content type. 只要他们的连接没有任何其他参数(例如订阅日期等),我就会在学生的内容类型中选择实体引用。

Both of these are valid, and there's an additional option of having a corresponding reference field on each of your content types. 这两个都是有效的,还有一个附加选项是在每种内容类型上都有一个相应的引用字段。

The best option comes down to maintain ability for your editors. 最好的选择就是维护您的编辑人员的能力。

As per @c1u31355, if there is additional "connection" metadata, then a third content type is the way to go (or maybe a paragraph). 按照@ c1u31355,如果还有其他“连接”元数据,则第三个内容类型是处理的方法(或者可能是一个段落)。

If it's a straight connection A <> B, and you only want the reference in one place, then ask yourself where is most convenient to add that data? 如果是直接连接A <> B,而您只希望将引用放在一个位置,请问问自己,在何处添加该数据最方便? Is it easier to create a course and then link to 30 students, or are you wanting to add courses to students as you create them? 创建课程然后链接到30个学生是否更容易,还是要在创建学生时将课程添加到学生? Quicker at creation, but harder to maintain. 创建起来更快,但维护起来却更困难。

Either way, using IEF as I suggested in one of your other questions will help. 无论哪种方式,使用我在其他问题之一中建议的IEF都会有帮助。

As a final thought, having a 3rd content type could cause all sorts of issues unless you control the fields well, something like limiting it to only having one student with a bunch of courses, or vise versa, and in that case maintenance will be easier by just putting a reference field on the content type where you have restricted the field to one value. 最终的想法是,除非您很好地控制字段,否则拥有第三种内容类型可能会导致各种问题,例如将其限制为仅允许一个学生学习一门课程,反之亦然,在这种情况下,维护会更容易只需在内容类型上放置一个引用字段,即可将字段限制为一个值。

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

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