如何实现评论引用功能?
我们允许在我们的网站(如博客)上发表评论。现在我们想改变它,以便人们可以评论其他评论。
我们的表看起来像这样:
ID | CommentID | Comment | User | Date
-------------------------------------------------------------------------
1001 | 1 | Nice site | Me | 20.01.2010
1001 | 2 | Thx! | You | 21.01.2010
我提出了两个选项:
1)创建一个 ParentCommentID 并用它决定评论如何从上到下列出。
2) 将您正在评论的评论与您自己的评论一起存储,并带有一些 [QUOTE] 标签或其他内容。
有什么好的建议来解决这个问题吗?
We allow commenting on our website (like a blog). Now we want to change it so that people can comment other comments.
Our table looks something like this:
ID | CommentID | Comment | User | Date
-------------------------------------------------------------------------
1001 | 1 | Nice site | Me | 20.01.2010
1001 | 2 | Thx! | You | 21.01.2010
I came up with two options:
1) Create a ParentCommentID and with that decide how the comments are listed from top to bottom.
2) Store the comment you're commenting together with your own comment with some [QUOTE]-tags or something.
Any good tips to solving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会使用一个
ParentCommentID
作为返回该表的CommentID
的外键。这将强制引用完整性,并且您将避免一些重复。如果十个人引用一条评论,则使用选项 2 将该评论复制十次。在选项一中,您只有 10 个 FK。
单个评论中允许的字符越多,选项 2 中每个引用的重复内容就越多。
选项 1 还允许您进行更多报告。您可以更轻松地查询以找出哪些评论被引用最多。
I would go with a
ParentCommentID
that acts as a foreign key back to that table'sCommentID
. That will enforce referential integrity, and you'll avoid some duplication.If ten people quote a comment, it is replicated ten times with option 2. In option one, you only have ten FKs.
The more characters you allow in a single comment, the more duplication you'll have per quote with option 2.
Option 1 also allows you to do more reporting. You can more easily query to find out which comments are quoted the most often.
您真正要问的是您希望数据库表如何标准化。我想说,这是一个基于诸如您的网站有多大以及您认为应该进行标准化的程度等因素的判断。使用类似的示例,在我们的论坛中,我们将发布线程的用户名存储在线程表中 - 出于性能原因,这是非规范化的 - 有时您只是不想进行大量连接。维基百科关于数据库规范化的文章有更详细的介绍。
我个人的看法是,我会存储带有引用的评论,因为这可以防止人们更改他们已经编写的内容(忍者编辑)来混淆争论/讨论;)
What you're asking really is how normalized you want your database table to be. I would say this is a judgement call based on factors such as how large your website is and how far you believe you should take normalization. To use a similar example, in our forums we store the username of who posted a thread in the thread table - this is de-normalized for performance reasons - sometimes you just don't want to be doing lots of joins. The Wikipedia article on database normalization goes into more detail.
My personal take is I would store the comment with the quote inside it, as this prevents people from changing something they've already written (ninja editing) to obfuscate arguments/discussions ;)
听起来我们需要先收集更多需求!
Sounds like we need to do some more requirements gathering first!
Parentid 是一个不错的选择。您还可以存储时间和日期,以找出已发布回复的层次结构,甚至可以使用 CommentID 字段。
The parentid is a good option. You can also store time along with date to find out the hierarchy of posted reply or you can even use the CommentID field for it.
如果页面上未显示评论 ID,您始终可以应用 IP 地址或应用程序版本等编号方案。 IE,CommentID 2.1是第一条评论对第二条评论,2.2是第二条评论对第二条评论。因为不需要太多的字符串标记器来确认 CommentID 3.1.7 是帖子的第一条评论和第二条评论的第七条评论。一旦将其标记为整数,排序就变得轻而易举。
If the comment ID isn't displayed on the page you could always just apply a numbering scheme like an IP address or an application version. I.E., CommentID 2.1 is the first comment on the second comment, and 2.2 is the second comment on the second comment. As it wouldnt take much of a string tokenizer to acknowledge CommentID 3.1.7 is the seventh comment on the first comment on the second comment of a post. Sorting becomes a breeze once you tokenize it into integers.