简体   繁体   English

PHP / MySQL关系表建议

[英]PHP/MySQL relationship table advice

I was just wondering if I might get some advice on the following: 我只是想知道是否可以从以下方面获得一些建议:

I'm building a site in CodeIgniter in which exists a content type "portfolio_item". 我正在CodeIgniter中构建一个网站,其中存在内容类型“ portfolio_item”。 The same portfolio item may be displayed in 3 places via checkbox controls: Homepage, Member Page and Client Page. 通过复选框控件,可以在3个地方显示相同的项目组合项目:“主页”,“成员页面”和“客户页面”。

I'm just wondering what the best way to implement this relationship in the database is. 我只是想知道在数据库中实现这种关系的最佳方法是什么。 I could have 3 relationship tables for each of the above scenarios, but to me this seems overkill (the site is quite small). 对于上述每种情况,我都可以有3个关系表,但是对我来说,这似乎有点过头了(该站点很小)。

I was thinking of using one relationship table with the following fields: 我正在考虑使用一个具有以下字段的关系表:

type (homepage, client or member)
show_on_id
portfolio_id

The intent of the type field is to determine which table to retrieve the "show_on_id" from (either clients or member), if the type is homepage then show_on_id is not required. 类型字段的目的是确定从哪个表(客户端或成员)中检索“ show_on_id”,如果类型是主页,则不需要show_on_id。

Is there any obvious disadvantage of doing it this way? 这样做有明显的缺点吗?

Yes, there is a bit of a disadvantage. 是的,有一点缺点。 You could end up with multiple rows for the same setting, each might contradict each other. 对于相同的设置,您可能最终会有多行,每行可能彼此矛盾。 So, how you insert rows into this table, is very important. 因此,如何在表中插入行非常重要。

If you will not add any more sections, might as well have the portfolio table: 如果您将不再添加任何部分,则可能还会有投资组合表:

CREATE TABLE `portfolio`
(`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`content` TEXT NULL,
`showHome` BOOLEAN NULL,
`showClient` BOOLEAN NULL,
`showMember` BOOLEAN NULL)

And then the table which links the users to their portfolios, 然后是将用户链接到其投资组合的表格,

CREATE TABLE `portfolio_user`
(`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`portfolio` INT NOT NULL,
`user` INT NOT NULL)

If you are going to add more places where the portfolio can be displayed later, or if these places are dynamic, your method will work. 如果要添加更多可以稍后显示投资组合的位置,或者这些位置是动态的,则您的方法将起作用。 I would just change 'type' to 'place' as that is easier to understand, then either use ENUM or another table to define the places that the portfolio can be shown. 我只是将“类型”更改为“位置”,因为这更容易理解,然后使用ENUM或另一个表来定义可以显示投资组合的位置。

Can there be one or many portfolio items for each location? 每个地点可以有一个或多个投资组合项目吗?

If many, use link tables, if only one use direct foreign key field to link to the portfolio. 如果有很多,则使用链接表;如果只有一个,则使用直接外键字段链接到投资组合。

I would advice against using one link table for all three based on personal experience with exactly such designs and the problems that can surface later. 我建议不要根据个人经验以及完全相同的设计以及以后可能出现的问题,针对所有三个链接表使用一个链接表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM