假设我们有以下部分 ER 图:

请注意,该attachments表随后将用于消息、提交、作业和讲座的附件。
attachments问题在于和messages、submissions和之间的 3 个一对一关系assignments。根据 CakePHP 的约定,Message belongsTo Attachment(and Attachment hasOne Message) 因为Message包含外键(同样的事情适用于其他 2 个关系)。当然,这样说Message hasOne Attachment(和那个Attachment belongsTo Message)更有意义。如果我们只有messagesand attachments,很容易通过将外键移动到来“正确定位attachments”关系。
但问题又是 , messages, submissions,assignments和lectures与同attachments一张表有关系。获得“语义正确”关系的一种方法是使用 4 种不同的Attachment模型:MessageAttachment、SubmissionAttachment、AssignmentAttachment和LectureAttachment。
假设我们只对检索某个消息、提交或分配的附件感兴趣,那么使用这些语义反转的关联是否可以,或者我们应该通过分离成上述 4 个不同的模型来正确定位它们?Attachment