0
votes

If I am creating a database and I have two tables with a many-to-many relationship, I would create a third table to represent the relationship.

But how should I implement the above data model in Drupal 8, should I create three content types? so my question is, are content types in Drupal 8 the same as tables in a database, or are they something else that should be used in a different way?

2

2 Answers

0
votes

To store data in a custom table in Drupal 8 you're going to want to define a custom schema and expose it through a hook. Here is Acquia's documentation on that process. This is not the same as creating a content type.

Content type data is stored across several tables that are shared by other content types (such as node and node_revision) and manipulating these tables is not easy.

The quickest way to add a content type is through the method recommended in Adding a Content Type from chapter 6 of the Drupal 8 User Guide. This allows for all the standard GUI methods for manipulating data structures.

0
votes

As Isaiah says, content types in Drupal are not technically the same as tables in a database as they use many tables. But!

In your case, if you have something like table A with:

- name
- email
- phone
- id

And a second table, table B with:

- company
- something else
- etc…
- id

and are looking at a third that would likely be something like:

- id
- table A id
- table B id

Then the answer is yes.

In this case you would have content type A with fields for name, email, phone, and then an entity reference field pointing to content type B with the company fields.

You can reference in any direction with the entity reference field.

You might also find "Is there a way to input the data of multiple content types" helpful.