如何实现只有1个深度级别的评论系统

时间:2021-04-10 14:34:59

I've read that the best solution for comment system is Managing Hierarchical Data in MySQL

我已经读过评论系统的最佳解决方案是在MySQL中管理分层数据

However, I plan to allow replies only for the top comments - only one depth level. In this case, the structure become much more simpler than a complicated tree.

但是,我计划只允许回复最高评论 - 只有一个深度级别。在这种情况下,结构变得比复杂的树更简单。

Is there any other way to implement it in this scenario? Something easier, simpler, quicker?

在这种情况下,还有其他方法可以实现吗?更容易,更简单,更快捷?

1 个解决方案

#1


2  

A implementation for 1 level is the same as a implementations for N levels.

1级的实现与N级的实现相同。

The only difference is that you only use 1 level.

唯一的区别是你只使用1级。

A possible implementation looks like this: put all the comments in 1 table, and use a foreign key to the blog article or whatever you commenting on. The nesting is stored in the ParentCommentId. Id its NULL it is a root comment, if it has a value if is a comment on a other comment.

可能的实现如下所示:将所有注释放在1个表中,并使用外键访问博客文章或任何您评论的内容。嵌套存储在ParentCommentId中。如果它是NULL,那么它是根注释,如果它有值,如果是对其他注释的注释。

It is very easy then to make a simple comment form and store the results in the database.

然后,可以很容易地创建一个简单的注释表单并将结果存储在数据库中。

The table structure could look like this:

表结构可能如下所示:

CREATE TABLE `Comments` (
  `CommentId` int(11) NOT NULL AUTO_INCREMENT,
  `BlogId` int(11) NOT NULL,
  `ParentCommentId` int(11),
  `Content` text NOT NULL,
  `Name` varchar(64) NOT NULL DEFAULT '',
  `EMail` varchar(64) DEFAULT NULL,
  `Url` varchar(64) DEFAULT NULL,
  `IP` varchar(20) NOT NULL DEFAULT '',
  `Created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `Modified` datetime DEFAULT NULL,
  PRIMARY KEY (`CommentId`),
  KEY `FK_Blog` (`BlogId`),
  CONSTRAINT `FK_CommentBlog` FOREIGN KEY (`BlogId`) REFERENCES `Blogs` (`BlogId`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#1


2  

A implementation for 1 level is the same as a implementations for N levels.

1级的实现与N级的实现相同。

The only difference is that you only use 1 level.

唯一的区别是你只使用1级。

A possible implementation looks like this: put all the comments in 1 table, and use a foreign key to the blog article or whatever you commenting on. The nesting is stored in the ParentCommentId. Id its NULL it is a root comment, if it has a value if is a comment on a other comment.

可能的实现如下所示:将所有注释放在1个表中,并使用外键访问博客文章或任何您评论的内容。嵌套存储在ParentCommentId中。如果它是NULL,那么它是根注释,如果它有值,如果是对其他注释的注释。

It is very easy then to make a simple comment form and store the results in the database.

然后,可以很容易地创建一个简单的注释表单并将结果存储在数据库中。

The table structure could look like this:

表结构可能如下所示:

CREATE TABLE `Comments` (
  `CommentId` int(11) NOT NULL AUTO_INCREMENT,
  `BlogId` int(11) NOT NULL,
  `ParentCommentId` int(11),
  `Content` text NOT NULL,
  `Name` varchar(64) NOT NULL DEFAULT '',
  `EMail` varchar(64) DEFAULT NULL,
  `Url` varchar(64) DEFAULT NULL,
  `IP` varchar(20) NOT NULL DEFAULT '',
  `Created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `Modified` datetime DEFAULT NULL,
  PRIMARY KEY (`CommentId`),
  KEY `FK_Blog` (`BlogId`),
  CONSTRAINT `FK_CommentBlog` FOREIGN KEY (`BlogId`) REFERENCES `Blogs` (`BlogId`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;