I need to develop an application that supports “schedules”. Example of schedules:
- Jan 1, 2011 at 9am
- Jan 1, 2011 from 9am to 10am
- Every Monday at 9am, from Jan 1, 2011
- Every Monday at 9am, from Jan 1, 2011 to Feb 1, 2011
- Every Monday at 9am, from Jan 1, 2011 for 10 occurrences
- etc.
If you have seen Outlook’s scheduler, that’s basically what I need. Here’s a screen shot of their UI: http://www.question-defense.com/wp-content/uploads/2009/04/outlook-meeting-recurrance-settings.gif
How would I model such information in a database? Keep in mind that I also need to query this, such as:
- What are the scheduled events for today?
- What are the next 10 dates/times for a particular recurring scheduled event?
- Etc.
I’m using PHP/MySQL, but am open to alternative solutions. Suggestions?
My personal opinion is to create all the events separately, with a start and end date. Then generate a unique identifier for the event (perhaps the event ID of the first you create) and assign it to all events (so you know they are somehow linked).
Advantages:
and create them all only once)
event, and then rebuild them all – remove and re-create)
Disadvantages:
— appended from here —
Proposed Model:
Table Event
idbig int (auto increment)ref_idbig int (this is kind of foreign key to the id)date_startdatedate_enddatetitlestringsaved_recurrencetextImagine you have an event repeating 4 weeks every Wednesday and Friday:
ref_id=0 andsaved_recurrence=saved JSON object and get the id that was used (auto incremented)ref_id=idref_id(saved_recurrencecan be empty here)You should now have 8 events (2 every week for 4 weeks) that have the same
ref_id. This way it’s easy to fetch events in any date interval. When you need to edit an event, you just check forref_id. If it’s 0 it’s a single isolated event. If not, you have a ref_id that you can search to get all event instances.