CakePHP mysql 数据库命名约定问题

发布于 2024-10-11 16:55:02 字数 843 浏览 0 评论 0 原文

首先:我已经阅读了 CakePHP 食谱中的命名约定规则,所以不要引导我去那里:)我可能理解错误,如果是这样,请纠正我;)

我遇到的问题是我有表:

  • items
  • quests

然后我有表:

  • item_has_quest_requirements
  • quest_has_item_requirements
  • quest_has_item_rewards
  • quest_has_item_recommends

这只是 HABTM 表,有 2 个主键连接这 2 个表。现在,其中一些也有自己的特定列,这就是为什么我没有将它们放入同一个表中。例如,item_has_quest_requirements 只有键,但 quest_has_item_requirements 也有 amount 字段,奖励和推荐也是如此。

但是,如果我遵循 CakePHP 命名约定,我不应该使用复合主键,并且如果我想将 2 个表作为 HABTM 表连接在一起,我应该按表名称的字母顺序命名这些表。这意味着据我所知,这些表将全部变成: items_quests 表,这是不可能的。

唯一的方法是创建全新的额外表,例如: quest_requirements 其中包含 amount 列,然后使 HABTM 表类似于:items_quest_requirements

这就是我一直在脑海中滚动的内容,所以你能启发一下什么是最好的方法吗?

First: I have read the naming convention rules from CakePHP cookbook, so dont direct me there :) I might though be understanding something incorrectly and if so please correct me ;)

The problem Im having is that I have tables:

  • items
  • quests

Then I HAD tables:

  • item_has_quest_requirements
  • quest_has_item_requirements
  • quest_has_item_rewards
  • quest_has_item_recommends

Which are just HABTM tables with 2 primary keys connecting these 2 tables. Now, some of them also have their OWN specific columns also which is why I had not put them into the same table. For example, item_has_quest_requirements has just the keys, but quest_has_item_requirements has also amount field as do rewards and recommends aswell.

However, if I follow CakePHP naming conventions, I should NOT use composite primary keys and if I wanted to connect 2 tables together as HABTM tables, I should name the tables in alphabetical order by table names. Which means AFAIK that these tables would ALL become: items_quests tables which is impossible.

Would the only way be then to create totally new extra table like:
quest_requirements which would have the amount column and then make the HABTM table like: items_quest_requirements ?

This is what I have been rolling in my mind so could you enlighten a bit what would be the best approach?

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

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

发布评论

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

评论(2

GRAY°灰色天空 2024-10-18 16:55:02

当两个表之间有多个关联时,没有命名约定。在这种情况下,您必须在模型中手动指定涉及的表,请参阅 http://book.cakephp.org/view/1039/Associations-Linking-Models-Together#hasAndBelongsToMany-HABTM-1044了解详细信息。

但是,您提到一些连接表将包含附加列。对于这种情况,使用 加入模型(也称为“hasMany through”)而不是 HABTM 关联。

There are no naming conventions for when you have multiple associations between two tables. In this case you have to specify the involved tables manually in your models, see http://book.cakephp.org/view/1039/Associations-Linking-Models-Together#hasAndBelongsToMany-HABTM-1044 for details.

However, you mention some join tables will contain additional columns. For such a scenario it might be a better approach to use Join Models (also known as "hasMany through") instead of HABTM associations.

陈甜 2024-10-18 16:55:02

尝试使用 http://cakeapp.com 重现它,它会强制您使用蛋糕命名约定。并允许您随后下载数据库模式。

Try to reproduce it with http://cakeapp.com, it forces you to use the cake naming conventions. And allows you to download the DB schema afterwards.

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