使用“语义反转”可以吗? CakePHP 中的关联,例如 Message blongsTo Attachment?

发布于 2024-09-09 03:48:52 字数 1015 浏览 4 评论 0原文

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

包含 5 个实体的 ER 图:消息、提交、附件、作业和讲座

请注意,attachments 表随后将用于消息、提交、作业和讲座的附件。

问题在于附件消息提交作业之间的3个一对一关系。根据 CakePHP 的约定,Message ownsTo Attachment(和 Attachment hasOne Message),因为 Message 包含外键(同样的情况也适用于其他 2 个外键)关系)。当然,说Message hasOne Attachment(并且Attachment属于Message)更有意义。如果我们只有消息附件,那么通过将外键移动到附件来“正确定位”关系就很容易

但问题又是消息提交作业讲座与相同的有关系附件表。获得“语义正确”关系的一种方法是使用 4 种不同的 Attachment 模型:MessageAttachmentSubmissionAttachmentAssignmentAttachmentLectureAttachment

假设我们只对检索特定消息、提交或作业的附件感兴趣,是否可以使用这些语义颠倒的关联,或者我们应该正确定位 > 如上所述,将 Attachment 分成 4 个不同的模型?

Suppose that we have the following partial ER diagram:

An ER diagram with 5 entities: messages, submissions, attachments, assignments, and lectures

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-09-16 03:48:52

语义也指的是语言的自然语义。但您正在谈论一个具有不同语义的特定技术场景。

那就完全没问题了。这些只是名字。如果您有 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.

不念旧人 2024-09-16 03:48:52

警告:这个答案有点类似于“你应该能够使用 MVC 框架来做到这一点”。我不熟悉 CakePHP,简单地查看文档我不知道如何实际执行以下操作。

在 Perl DBIx::Class(我熟悉的)中,可以在“属于”关系中施加额外的约束,而不仅仅是 fk 匹配。我将通过向 attachment 添加一个字段 type 来解决这个问题,该字段指示对象的类型(例如,messagelecture< /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 field type to attachment, which indicates the type of object (e.g., message, lecture) that owns this particular attachment and then give attachment a foreign_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文