views:

357

answers:

2

I want to be able to have notifications on my site, similar to the way SO does it. I have looked for a good table structure to do this, but I cant seem to figure it out.

I was thinking something like this.

Notifications

id, notification_type_id, user_id, type_id

Notification Types

id, notification_text

Where the notification type would relate to either a new post, a new comment, or whatever features I add later down the line... User Id would relate to whoever the notification is for. Type_id and notification type would go hand in hand, so if the notification_type was a new comment, the type_id would be the comment_id to go to.

This seems good to me, but i want to be able to notify ALL users when something changes.. like on facebook when you comment on something, you get a notification that someone else has also commented on the same thing after you.

I cant seem to figure this out... Help wanted

Thanks

EDIT: The way I thought about it was, the notification_type_id would map to the notification_type table, which would hold the text for each notification ("You have a new comment", "blah blah blah has also commented on blah blah blah", etc.), type_id would map to the primary_id of whatever the comment is about. for example, if the notification is saying you have a new comment on your post, then the type_id would be the primary id of the post for easy linking.. IDK, it was just a thought.

A: 

Can you detail a little more what is each column, because your text is not very precise, since you mention "notification type" and that is not a column in your example. What column points to id in the table notifications types: notification_type_id or type_id?

JorgeLarre
I may be missing something, but the notification to all users does not seem hard to achieve: you just join the notifications table and the table where you store the users, and you will have it set. You can even filter on the users, so not everyone get the notification. You can store the resulting rows in a different table, where you will have the user_id and the notification_id. Was this what you were looking for?
JorgeLarre
The notifications are not sent through me.... they are event notifications that are populated every time an event pertinent to the user happens... Like, if somebody were to answer your question on SO, or comment on your answer... two different notifications. Plus, even harder is to send a notification to everybody who has already answered a queastion that a new answer has been posted by someone else.
BDuelz
+4  A: 

Look at these stackoverflow questions:
Facebook like notifications tracking (DB Design)
Database design to store notifications to users

Pay particular attention to this answer that has a clean solution of your request.

systempuntoout