Reputation: 1369
I'm building timeline, that will show all things that users does, ( events ). Example events:
User has joined site ( registration )
User has added post
User created public event
User joined some random group.
User has added a comment into forum or group.
Problem is that i don't know how to structure database, that will keep all events and show them in timeline.
Visual example:
I'm using Laravel with eloquent. I thought on all actions i could raise events, and them listen to them and add all events to database events table.
At example, when user registers, event is raised, and data is put to database, then script gets all events from database and structures them by date.
But problem is that all events has different content, at example, when user registers, I need to store only registered user foreign key that points to users table, but when user creates a post, I need to store his foreign key, and foreign key that points to created post.
So question is how, to structure database, that will keep all events for displaying them on timeline, assuming that event contents for each action can be different?
Upvotes: 3
Views: 2999
Reputation: 598
I would use polymorphic relationships:
http://laravel.com/docs/4.2/eloquent#polymorphic-relations
So your events table would look like this
Events
id
user_id
eventable_id
eventable_type
event_class
You would have a user relationship, and an eventable polymorphic relationship, which could point to a post, or back to a user (for registration).
I also added an event_class field, which could store the name of a class to handle how the event displays (you could have a class to handle New Post events, for example). I might even omit that and resolve the handler class somehow from the eventable_type column.
Depending on how you want it to work, you could also store the title and body in the database table, but I'd prefer to use a class to generate that on the fly.
Upvotes: 2