Suppose that we have the following partial ER diagram:
Notice that the attachments
table will subsequently be used for the messages', submissions', assignments', and lectures' attachments.
The problem is with the 3 one-to-one relationships between attachments
and messages
, submissions
, and assignments
. According to CakePHP's conventions, Message belongsTo Attachment
(and Attachment hasOne Message
) because Message
contains the foreign key (the same thing applies to the other 2 relationships). Of course, it makes more sense to say that Message hasOne Attachment
(and that Attachment belongsTo Message
). If we only had messages
and attachments
, it would be easy to "properly orient" the relationship by moving the foreign key to attachments
.
But the problem again is that messages
, submissions
, assignments
, and lectures
have relationships with the same attachments
table. One way to get the "semantically correct" relationships is to have 4 different Attachment
models: MessageAttachment
, SubmissionAttachment
, AssignmentAttachment
, and LectureAttachment
.
Assuming we are only interested in retrieving the attachment of a certain message, submission, or assignment, is it OK to use these semantically reversed associations, or should we properly orient them by separating Attachment
into 4 different models as mentioned above?