通知表设计


Notifications table design?

我正在尝试为我的社交网络创建一个通知系统。但我一直停留在数据库设计上。所以当一个用户评论另一个用户时,我想显示"X评论了你的帖子"。或者当有人关注另一个时,我想要显示一个通知。

到目前为止,这是我的桌子:

CREATE TABLE IF NOT EXISTS `notifications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `notification_id` int(11) NOT NULL,
  `text` text NOT NULL,
  `read` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

我感到困惑的是,当有人跟踪另一个人时,我应该插入一条记录吗?喜欢就在有人点击关注按钮之后?如果是,我应该在行中插入什么?

我使用的是带有Laravel框架的PHP。

由于这是一个没有完美解决方案的开放式问题,我建议采用以下结构:

事件表

id    type          text
-----------------------------------------------
1     comment       commented on your post
2     follow        followed you
etc

那么notification table将如下(我想不出更好的字段名称来传达这个想法):

通知表

id      user_to_notify       user_who_fired_event  event_id    seen_by_user
--------------------------------------------------------------------------- 
 1            12                 13                    2        yes
 2            13                 12                    1        no
 3            1                  15                    1        yes

seen_by_user可以是布尔值。

使用上述结构的优点是,您可以在没有复杂SQL查询的情况下回答以下查询:

  1. 用户Y对用户X帖子的评论总数
  2. 用户阅读或点击通知的模式。例如,他可能不会点击关注他的人的通知
  3. 如果用户偏好不被通知某些事件,则可以根据event_id进行筛选

一旦X对Y的帖子发表评论,您就在通知表中输入一个条目,然后在comments table中输入带有评论的条目。我希望这在某种程度上有所帮助!

我知道我很晚了,但对于那些仍在寻找的人来说。。我正在构建类似的东西,我正在使用它,它对我来说很好

    public function up()
{
    Schema::create('notifications', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('user_id'); // set relationship in user model 
        $table->string('user_to_notify'); //the user who will recive this notification
        $table->string('type'); //follow ,comments etc
        $table->string('data'); //follow id ,comment id etc ,you can set relationship in model about this
        $table->integer('read'); 
        $table->timestamps();
    });
}

对于任何使用Laravel 5.4或更高版本的人(我不确定更低版本):

您可以键入php artisan notifications:table,它将创建一个带有LaravelNotifications所需的基本字段的迁移。

更多信息,请访问https://laravel.com/docs/5.5/notifications

我建议这样做:

Table person (id, name, etc)
Table post (id, personid, text, etc)
Table comment (id, personid, postid, text, SeenByOP, etc)

CCD_ 6将是具有默认值CCD_ 7的比特字段。当OP查看注释时,该值将更新为1

我认为没有必要使用通知表。