1

In this thread I need a link from base table to join table it was suggested that it is better in Drupal to join two entities with a multi-value entity reference field than to use a relational style join table to join them (assuming many to many relationship). But, what about the case where there is a property of the association. E.g., stories win awards, but each award is or may be granted every year. So, in a relational design, one would use a join table to link a particular story with the award it had won and put the year into the association. What is the Drupal Way to do this with a multi-value entity reference field?

OK, here is a simplified diagram of the relationship between Story and Award showing the property on the association.enter image description here

  • FWIW, I am asking about the same site as in the other thread, but she is the one primarily responsible for the development and I just advise. I have deep experience in relational design, but the message I get here is that I should be thinking more OO. I believe she looked at relation, but came back to entity reference for some reason. I get the simplicity and directness of entity reference, but am not sure what to do with that association property. – Thomas Jun 7 '16 at 14:38
  • I also posted some advice in this meta thread. Specifically the advice to enhance the question with some images. The situation you now have created is joining over 5 tables; node > entity reference field > node > entity reference > node. This middle node invokes (useless) access checks and all with all this generates a lot of overhead. Relation is more difficult to use since it does not have a UI, but is more powerful. – Neograph734 Jun 8 '16 at 20:52
0

First, there is no Drupal way. Drupal is flexible and the same result can be achieved in many ways. If you feel comfortable using join tables, go for it. In the other thread OP wanted to use a relation between entities in View, what entity references offer out of the box, where a join table would not. Therefor that was suggested.

For a situation where a relation needs to have properties on its own, you might want to look into the Relation module. It provides a relation between two entities, but the relation can have properties (fields) on its own (such as as year).

I just don't know how stable the module is for Drupal 8, but it works great in Drupal 7.

Update after the question of Asy:

The entity principle in Drupal 8 is similar to 7, so it would not change the answer much, except that the relation module for Drupal 8 is not yet finished. The problem with this specific question is that the answer is spread over 3 different posts. The combined essense is: nodes are pretty 'resource heavy' entities, as they invoke access checks, path aliasing, etc. Using them just to attach a property to a 'relation' works, but consumes more resources than a system that was designed to do just this (the Relation module, which for instance avoids the node access check and path aliasing on the in-between node). Especially when combining 3 nodes with 2 fields, this becomes SQL heavy too. Nothing SQL can't handle, just don't do it too often. So in the end is all depends on the application. If this is not a page that gets much views, you might not even notice it.

A better alternative (but I did not test nor benchmark this) might be to create a custom entity using the ECK (Entity Construction Kit). These entities are presumably lighter than nodes. Combined with Drupal 8's default entity referencing and something like Inline Entity Form to fill the field on the relation could make quite a nice interface.

In the end however you still need to join like this:

Node > entity reference field > ECK entity > entity reference field > node
  v                                  v                                  v
fields                            fields                             fields

Where the relation module (if finished) would offer something that probably looks more like this:

Node > relation > node
  v       v         v
fields  fields    fields
  • Hi, we're in Drupal 7. I tried Relation first but there is even LESS support for Relation than for Entity Relationship, so I thought I would use something with at least a few people who might be a resource. Thank you for all your troubles :) – TdeV Jun 9 '16 at 22:19
  • @TdeV No problem :) The info got a bit scatteted over different posts. But I hope it is enough for you to work something out. – Neograph734 Jun 9 '16 at 23:01
  • I'm sure to keep you posted :D – TdeV Jun 9 '16 at 23:30
  • what about drupal 8? – Asy Oct 17 '16 at 15:12
  • I've updated the answer. It is a bit of speculation , but hopefully answers your question – Neograph734 Oct 17 '16 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.