0

我想将评论存储在一个灵活的类别和子类别系统中,目前正在为此设计数据库结构。我有一个想法如何做到这一点,但我不完全确定它是否不能做得更优雅和/或更高效。这些是我的想法 - 如果有人可以评论是否/如何改进,我将非常感激。

(为了保持这篇文章的简洁,我只列出了表格的重要字段)

1.) 评论存储在“评论”表中。它具有以下字段:

id: uniquite ID, auto-incrementing.
title: the title that will show up in <head><title>, etc.
stub: a version of the title without spaces, special chars, etc. so it can be part of the URL/URI
text: the actual content

2.) 所有类别都在同一个表“类别”中

id: unique ID, auto-incrementing.
title: the full title/name of the categorie how it will be output on the website
stub: version of the title that will be shown in the URL/URI.
parent_id: if this is a subcategory, here is the categories.id of the parent category. Else this is 0.
order_number: simple number to order the categories by (for display in the navigation menu)

3.) 现在我需要一个指标,哪些评论属于哪些类别。可以是多个。我的第一个想法是在类别中添加一个“review_list”字段,并让它包含所有应该在这个类别中的reviews.id。但是,我认为在类别中添加和删除评论会很麻烦且“不雅”。所以我目前的想法是有一个表“review_in_category”并为每个评论类别关系都有一个条目。结构是:

id: Unique ID, auto-increment. 
review_id: the reviews.id 
category_id: the categories.id

因此,如果评论属于 3 个不同的类别,则会在“review_in_category”表中产生 3 个条目。

这个想法是,当用户打开 www.mydomain.de/animation/sci-fi/ 时,包装脚本会将 URL 分解成各个部分。如果它找到多个具有 category.stub = "sci-fi" 的类别,它将检查其中哪一个具有具有存根 "animation" 的父类别。一旦确定了正确的类别(大多数情况下存根都是唯一的,因此可以跳过此检查)我想从“review_in_category”中选择所有 review_id,其中 category_id 与包装脚本确定的匹配。所有的 review_id 都被放入一个数组中。一个循环将遍历该数组并组成 SELECT 语句,以通过“SELECT title, stub FROM reviews WHERE id=review_list[$counter]”列出所有评论标题(并使用存根值创建指向它们的链接),然后添加“

所以我的问题是: - 我创建一个可能包含大量“OR id =”部分的单个 SELECT 语句的方法是“优雅”和/或处理这种情况的有效方法,还是有更好的变体?- 使用“分类”样式表 (review_in_category) 是否有意义,或者将“成员”/“关系”直接存储在评论或类别表中会更好吗?- 任何其他想法......我刚开始学习这些东西并感谢任何反馈。

谢谢

4

1 回答 1

0

你的设计看起来不错。

要检索一个类别中的所有评论,您应该使用连接:

SELECT reviews.title, reviews.stub FROM reviews, review_in_category WHERE reviews.id = review_in_category.review_id AND category_id = $category
于 2012-11-26T01:06:29.303 回答