具有 HABTM 关系的 HABTM 连接表

发布于 2024-10-05 08:24:40 字数 417 浏览 9 评论 0原文

如果我在 HTBTM 关系中有以下数据库表名称:

**dressess**
id

**dressess_categories**
id
dress_id
category_id

**categories**
id

我如何为 HABTM 关系创建数据库表名称需要在 dressess_categories 表中,例如:

**dressess_categories**
id

**colors_dress_categories**   <--- It is named right?
id
dress_category_id   <--- It is named right?
color_id

**colors**
id

提前致谢。

If i have the following database table names in a HTBTM relantionship:

**dressess**
id

**dressess_categories**
id
dress_id
category_id

**categories**
id

How do I have to create the database table names for an HABTM relationship need it in dressess_categories table, for example:

**dressess_categories**
id

**colors_dress_categories**   <--- It is named right?
id
dress_category_id   <--- It is named right?
color_id

**colors**
id

Thanks in advance.

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

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

发布评论

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

评论(1

此刻的回忆 2024-10-12 08:24:41

首先,连接表需要按字母顺序排列模型名称,以便 CakePHP 能够自动识别它们:

dresses_categories /* incorrect */
categories_dresses /* correct */

现在解决手头的问题:如果我正确理解您的问题,您正在尝试在 Color< 之间创建 HABTM 关联/code> 用于不同 HABTM 关系的模型和连接表?这是不可能的。 (或者如果是的话,这将是一场蛋糕黑客马拉松)。

首先,停止考虑数据库表之间的关系。 CakePHP 模型之间定义关系,并由表驱动它们。为了完成此任务,您需要做的是创建四个模型:

Dress
Category
DressCategory
Color

像这样定义关系:

Dress         -> hasMany   -> DressCategory
Category      -> hasMany   -> DressCategory
DressCategory -> belongsTo -> Category, Dress
DressCategory -> HABTM     -> Color
Color         -> HABTM     -> DressCategory

以及五个具有适当外键的表:

dresses
-------
id

categories
----------
id

dress_categories
----------------
id
dress_id
category_id

colors
------
id

colors_dress_categories
-----------------------
id
color_id
dress_category_id

First, the join table needs to have the model names in alphabetical order for CakePHP to recognize them automagically:

dresses_categories /* incorrect */
categories_dresses /* correct */

Now for the problem at hand: If I understand your question correctly, you are trying to create a HABTM association between a Color model and the join table that is used for a different HABTM relationship? This is not possible. (or if it is, it will be a cake-hack-a-thon).

First, stop thinking of relationships as they relate between database tables. Relationships are defined between CakePHP Models, and the tables drive them. What you will need to do in order to accomplish this task is make four models:

Dress
Category
DressCategory
Color

Define relationships like so:

Dress         -> hasMany   -> DressCategory
Category      -> hasMany   -> DressCategory
DressCategory -> belongsTo -> Category, Dress
DressCategory -> HABTM     -> Color
Color         -> HABTM     -> DressCategory

And five tables with the appropriate foreign keys:

dresses
-------
id

categories
----------
id

dress_categories
----------------
id
dress_id
category_id

colors
------
id

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