Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm developing a web calendar in php (using Symfony2) inspired by iCal for a project of mine. At this moment, I have two classes: a class "Calendar" and a class "CalendarCell".

Here you are the two classes properties and method declarations.

class Calendar
{
    private $month;
    private $monthName;
    private $year;
    private $calendarCellList = array();
    private $translator;

    public function __construct($month, $year, $translator) {}               

    public function getCalendarCellList() {}

    public function getMonth() {}

    public function getMonthName() {}

    public function getNextMonth() {}

    public function getNextYear() {}

    public function getPreviousMonth() {}

    public function getPreviousYear() {}

    public function getYear() {}

    private function calculateDaysPreviousMonth() {}

    private function calculateNumericDayOfTheFirstDayOfTheWeek() {}

    private function isCurrentDay(\DateTime $dateTime) {}

    private function isDifferentMonth(\DateTime $dateTime) {}
}

class CalendarCell 
{
    private $day;
    private $month;
    private $dayNameAbbreviation;
    private $numericDayOfTheWeek;
    private $isCurrentDay;
    private $isDifferentMonth;
    private $translator;

    public function __construct(array $parameters) {}

    public function getDay() {}

    public function getMonth() {}

    public function getDayNameAbbreviation() {}

    public function isCurrentDay() {}

    public function isDifferentMonth() {}
}

Each calendar day can includes many calendar events (such as appointments or schedules) stored in a database. My question is: which is the best way to manage these calendar events in my classes? I think to add a eventList property in CalendarCell and populate it with an array of CalendarEvent objects fetched by the database.

This kind of solution doesn't allow other coders to reuse the classes without db (because I should inject at least a repository services also) just to create and visualize a calendar... so maybe it could be better to extend CalendarCell (for instance in CalendarCellEvent) and add the database features?

I feel like I'm missing some crucial design pattern!

Any suggestion will be very appreciated!

share|improve this question
    
Are these programming events (database triggers) or calendar events (holidays, appointments)? –  JeffO Oct 10 '12 at 11:38
    
Calendar events. I have just realized that the post is a bit ambigous... Time to edit! –  Gianluca78 Oct 10 '12 at 12:40
add comment

1 Answer 1

up vote 0 down vote accepted

to add a eventList property in CalendarCell and populate it with an array of CalendarEvent objects fetched by the database.

Note: This is not a PHP solution, instead its a solution based on a RDBMS approach.

Yes you can do what you suggested above. Other developers who don't want to use the events could just not call the method that loads the events. A calendar cell and an event are 2 separate things. Hence, I see them as 2 separate classes.

The associations between a cell and an event (as I would think of it) may be something like:

  • A calenderCell has 0,1 or more Events.

  • An Event belongs to one or more calendarCells (for example, in case of a conference or a task spanning many days).

The above represent a Many-To-Many association.

You need a third class "calendarEvnet-MM" that holds the PK of calendarCell and calendarEvents.

So you'd have:

calendarCell Table:

claendarCell_ID (Primary Key) ...

calendarEvent Table:

calendarEvent_ID (Primary Key) ...

calendarEvnet-MM Table:

calendarEvnet-MM_ID (Primary Key)

calendarCell_ID_FK (Foreign Key)

calendarEvent_ID_FK (Foreign Key) ...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.