CakePHP:HABTM + 1

发布于 2024-10-28 21:54:15 字数 409 浏览 2 评论 0原文

我有一个网站,可以从 tv.com 上抓取某些系列的所有剧集。

所以我的数据库有一个 User 表,一个 Show 表,一个 Episode 表,一个 Show_User 表(将节目与用户关联,HABTM),一个 Episode_Show 表(将剧集与节目关联,HABTM),一个 Episode_User 表(将剧集与节目,仅作为将其标记为“已观看”的一种方式)。

现在我想添加一种将剧集标记为“已下载”的方法。

目前,Episode_User 表有两个字段:Episode_Id 和 User_Id。我应该为此创建一个新表吗?或者只是向 Episode_User 表添加一个字段?

我正在使用 CakePHP 的 automagic 功能,并且并不特别想破坏它。但如果我必须这样做,我必须......

感谢您的任何建议。

I have a site that scrapes all the episodes from tv.com from certain series.

So my database has a User table, a Show table, an Episode table, a Show_User table (to associate shows with users, HABTM), an Episode_Show table (to associate episodes with shows, HABTM), a Episode_User table (to associate episodes with shows, only as a way of marking them as 'watched').

Now I want to add a way of marking an episode as 'downloaded' too.

So at the moment, the Episode_User table has two fields, Episode_Id and User_Id. Should I create a new table entirely for this? Or just add a field to the Episode_User table?

I'm using CakePHP's automagic features, and don't particularly want to break it. But if I have to, I have to...

Thanks for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

满地尘埃落定 2024-11-04 21:54:15

我不明白您为什么要为用户下载的剧集创建一个新表。对我来说,修改 Episode_User 表以具有用于观看的字段和用于下载的字段是最有意义的,因为它都与同一对实体、用户和剧集相关。

然而,每当我以这种方式存储有关两个表之间关系的信息时,我发现无论我使用什么框架,ORM 都不可避免地变得更加复杂,但我认为没有任何解决办法那里。

使用 CakePHP 来处理这些复杂的情况,请阅读有关模型行为的内容,Containable。 CakePHP 书中没有很好地记录它,但在需要使用 Episode_User 中的字段的情况下确实非常有用,例如,如果您需要查找已观看特定剧集但未下载的所有用户它。

另外,在阅读您的文章时,我想到您可以通过在节目和剧集之间建立 hasMany 关系来使数据模型更加简单。一个剧集永远不会属于多个节目,因此您的剧集表可能只有另一个字段 show_id,它与节目表相关,您甚至不需要 Episode_Show 表。

I don't see why you would want to create a new table for episodes a user has downloaded. To me it would make the most sense to modify the Episode_User table to have a field for watched and a field for downloaded, since it's all relating back to the same pair of entities, users and episodes.

However, any time I've stored information about a relationship between two tables in that manner, I've found that regardless of the framework I'm using, the ORM inevitably become more complicated, but I don't think there's any way around there.

With CakePHP for handling those kinds of complicated situations, read up about the model behavior, Containable. It's not very well documented in the CakePHP book, but is really quite useful in a situation where you need to use the fields in Episode_User, for example, if you needed to find all of the users that had watched a particular episode, but not downloaded it.

Also, it occurred to me, while reading your post, that you could possibly make your data model more simple by having a hasMany relationship between shows and episodes. An episode will never belong to more than one show, so your episodes table could just have another field, show_id, which related back to the show table, and you wouldn't even need the Episode_Show table.

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