修改评论系统的前序遍历
我正在尝试为博客制作一个评论系统。 我已经修改了预序遍历系统(使用本指南:http:// mikehillyer.com/articles/managing-hierarchical-data-in-mysql/)。
不过我有一些问题。 我不认为该指南解释了如何管理不同的博客文章以及添加不是回复的评论。
我的评论表如下所示:
+-------------+----------------------+-----+-----+
| comment_id | message | lft | rgt |
auto increment
+-------------+----------------------+-----+-----+
这是管理此问题的好方法吗:
我在评论表中添加了名为“blog_post_id”和“root”的列。 当我发表博客文章时,我会在评论表中添加一个条目,并将 blog_post_id 和 root 设置为 true。 然后,lft 是 comment_id,右侧是 comment_id + 1。
要加载博客文章的评论,我会找到 lft 和 rgt,其中 blog_post_id = x 且 root = true,然后选择 lft 和之间的所有评论rgt 其中 blog_post_id 是 x...
我刚刚想出了这个方法,所以我很确定一定有更好的方法。
谢谢
I'm trying to make a comments system for a blog. I have the modified preorder traversal system working (used this guide: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/).
I have a few issues though. I do not think that guide explains how to manage having different blog posts, and adding a comment that is not a reply.
my comments table looks like:
+-------------+----------------------+-----+-----+
| comment_id | message | lft | rgt |
auto increment
+-------------+----------------------+-----+-----+
Is this a good way to manage this:
I add column to my comments table called "blog_post_id" and "root". When I make a blog post I then add an entry into the comments table with the blog_post_id, and root set to true. Then, the lft is the comment_id and the right is the comment_id + 1.
To load the comments for a blog post I would find the lft and rgt WHERE the blog_post_id = x and root = true, then select all the comments between the lft and rgt where the blog_post_id is x...
I just came up with this method, so I'm pretty sure there must be a better way.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你“想出了”一个非常好的方法。 这是管理嵌套注释的标准方法。 关于这一点和实现也有相当多的文献。
请查看此处的示例实现: http://api.rubyonrails .org/classes/ActiveRecord/Acts/NestedSet/ClassMethods.html
You "came up" with a very good method. It is the standard way to manage nested comments. There is quite a bit of literature on this and implementations as well.
Take a look here for an example implementation: http://api.rubyonrails.org/classes/ActiveRecord/Acts/NestedSet/ClassMethods.html
添加
blog_post_id
列并将博客文章本身视为虚拟评论(评论树的根)怎么样? 然后它可以有多个孩子。使用此方法,您可以不加更改地使用该文章中的所有算法,但需要注意的是,您将
AND blog_post_id == foo
添加到所有查询中,并始终忽略根评论(因为它代表博客)本身。)How about adding a
blog_post_id
column and considering the blog post itself as a virtual comment (the root of the comment tree)? Then it can have multiple children.Using this method, you could use all the algorithms from that article unchanged, with the caveat that you add
AND blog_post_id == foo
to all the queries and always ignore the root comment (since it stands for the blog itself.)