0
votes

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:

  • 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?

2

2 Answers

0
votes

I think you're right, I would have suggested both ways.

As long as their connection doesn't have any additional parameters (e.g. date of subscription etc.), I would choose the entity reference within the students' content type.

0
votes

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).

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? 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? Quicker at creation, but harder to maintain.

Either way, using IEF as I suggested in one of your other questions will help.

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.