我想存储和检索社区站点的用户通知数据。当有人评论用户的帖子或有人关注用户时,应生成通知。我希望这种行为能够复制Facebook通知。所以它应该看起来像这样:
User A, User B and 3 other started following you.
User A, User Z and 2 others commented on your post Super Duper.
如果我创建一个用于保存通知的通用数组,那么它将如下所示:
$notification = array ($notification_id, notification_time, $read_status, $notification_recipient_id, $notification_initiator_id, $post_id=NULL, $comment_id=NULL); // This array represents a single notification
$notifications = array ($notification, ...); // This array is made up of many single notification array.
因此,如果要从$notifications数组(包含所有单个通知数组的数组)中检索数据,我将使用 for 循环并在根据时间对数组进行排序后回显结果。结果,这会给我这样的结果:
User A started following you.
User B started following you.
User C started following you.
User D started following you.
User E started following you.
User A commented on your post Super Duper.
User B commented on your post Super Duper.
User C commented on your post Super Duper.
User D commented on your post Super Duper.
所以,如果你看看我打算实现的结果和我从我目前的阵列设计中获得的结果,两者都是不同的。我可以通过对$notifications数组执行 N 个可能涉及扁平化、循环递归、排序等操作来达到预期的结果。但是,我认为我可以通过重新设计数据在数组中的存储方式来减少开销,以便在需要检索数据时我可以执行最少数量的操作。此外,由于在显示数据时需要考虑读取状态,因此实现我想要的结果会变得更加复杂。我请求有关设计数组结构的建议,以及如何从数组中检索数据以实现我想要的结果的示例。
你可以做以下操作:
- 选择类型为"关注"的通知,其中read_status = "not_read",限制 2
- 计算"关注"类型的通知
- 选择类型为"注释"的通知,其中read_status = "not_read",限制 2
- 计算"评论"类型的通知
每当有人阅读通知时,您都会将其设置为"已读"。
每当有人评论或关注某事时,您都会为所有相关人员创建通知。例如,这些人是参与讨论的所有人员,或者只是被回复的人。评论也是如此。
这样,您就可以格式化和显示此消息,而无需操作太多数据。(您可能可以打包这些请求以提高效率,但您明白了)
用户A、用户 B 和其他 3 人开始关注您。
用户 A、用户 Z 和 2 其他人评论了你的帖子超级骗子。
我想说的是,为这样的问题更改数据结构是矫枉过正的。使用这种方法,您将不断重新定义数据模型,这很快就会成为一场噩梦。数据模型应该反映应用程序的大约束,而不是像这样的"小"格式问题。