使用“语义反转”可以吗? CakePHP 中的关联,例如 Message blongsTo Attachment?
假设我们有以下部分 ER 图:
请注意,attachments
表随后将用于消息、提交、作业和讲座的附件。
问题在于附件
和消息
、提交
和作业
之间的3个一对一关系。根据 CakePHP 的约定,Message ownsTo Attachment
(和 Attachment hasOne Message
),因为 Message
包含外键(同样的情况也适用于其他 2 个外键)关系)。当然,说Message hasOne Attachment
(并且Attachment属于Message
)更有意义。如果我们只有消息
和附件
,那么通过将外键移动到附件来“正确定位”关系就很容易
。
但问题又是消息
、提交
、作业
和讲座
与相同的有关系附件表。获得“语义正确”关系的一种方法是使用 4 种不同的
Attachment
模型:MessageAttachment
、SubmissionAttachment
、 AssignmentAttachment
和 LectureAttachment
。
假设我们只对检索特定消息、提交或作业的附件感兴趣,是否可以使用这些语义颠倒的关联,或者我们应该正确定位 > 如上所述,将 Attachment
分成 4 个不同的模型?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
语义也指的是语言的自然语义。但您正在谈论一个具有不同语义的特定技术场景。
那就完全没问题了。这些只是名字。如果您有 A 和 B(附件和邮件),并且在您的情况下 B 有一个 A,那么说“附件有一条邮件”是正确的。
By semantics you also mean the natural semantics of the language. But you are talking about a particular, technical scenario that holds a different semantics.
That's perfectly fine. These are just names. If you have A and B (attachments and messages), and in your case B hasOne A, then it is the right thing to say attachment hasOne message.
警告:这个答案有点类似于“你应该能够使用 MVC 框架来做到这一点”。我不熟悉 CakePHP,简单地查看文档我不知道如何实际执行以下操作。
在 Perl DBIx::Class(我熟悉的)中,可以在“属于”关系中施加额外的约束,而不仅仅是 fk 匹配。我将通过向
attachment
添加一个字段type
来解决这个问题,该字段指示对象的类型(例如,message
、lecture< /code>)拥有这个特定的
附件
,然后给附件
一个foreign_id
。查询语法为 SELECT * from Attachment WHEREforeign_id == myId AND type == MyType。
看起来该结构可以让您消除
attachments_lectures
表,因为这似乎只是让您在每个讲座中拥有许多附件。如果您愿意,您还可以免费获得许多作业、提交和消息的附件。Warning: this answer is a bit along the lines of "you should be able to do this with an MVC framework". I am unfamiliar with CakePHP, and briefly looking at the documentation I can't figure out how to actually do the following.
In Perl DBIx::Class (which I am familiar with) it is possible to impose additional constraints in
belongs to
relationships than just fk matching. I would solve this problem by adding a fieldtype
toattachment
, which indicates the type of object (e.g.,message
,lecture
) that owns this particularattachment
and then giveattachment
aforeign_id
.The query syntax would be
SELECT * from attachment WHERE foreign_id == myId AND type == MyType
.It looks like that structure would let you eliminate the
attachments_lectures
table, since that seems to just be letting you have many attachments per lecture. You would also get for free being able to have many attachments for assignments, submissions, and messages if you were so inclined.