views:

274

answers:

2

I'm creating a calendar application in PHP with CodeIgniter. In the main calendar page, My model currently creates an array with each index being one day on the current month. That day is an array containing any events the user may have on that day. Now, should I construct the calendar array in the controller FIRST, then pass it to the model so the model can access the database and fill in all the events, or should I just create the calendar array and fill in the events ALL in the model?

$month = array( 'blank', 'blank', 'day1' => array( event 1, event2), 'day2' => ... );

+1  A: 

That's a difficult question, and I am not sure everyone would do the same way... But here's what I think should be taken into consideration :

  • If your Model's method is to be used from several part of your application (say, the controller that already calls it, a batch-programm, and a SOAP web-service interface), which may means being called from several Controller methods, the question is : what do you want the array to be like ?
    • If it needs to always contain the same thing ("full array") each time, then, to avoid copy-paste, you should put your code in the Model
    • If it needs to contains the "full thing" only in one case, then, your "special code" can be in the Controller -- or in another Model method, that would be called in the Controller, in that special case, and call the existing one to get the data.
    • Another way would be to add one layer, between the Controller and the Model... Might not be such a bad idea, after all...
  • If the array is considered as a "whole" only when the code you're talking about has been executed, then, I suppose it makes sense to put that code into the Model
  • If that code is part of some Business-logic rule, it has its place in the Model layer ; on the other hand, if it's only related to presentation, its place would be in the View.
  • If your Model method is called "getData", it should only return the array containing the "real" data, and not the one containing the "empty" days ; on the other hand, if it's called "getEventsByDay", it makes sense to have it return something for each day, even empty ones
  • You should try to have a coherent interface for your Model methods : if the one to save data takes some kind of array, the one the loads the data from your DB should return the same kind of array ; something like $model->save($model->load()) should work, I mean

In the end, there is no "good answer" : "it depends" ^^

I don't really know what I'd do in your case ; I suppose it kinda depends on the rest of the application... The question I'd ask myself is "does the notion of an empty day make sense for my Model, or is it just a matter of presentation ?"

Pascal MARTIN
A: 

I'd probably design it so it can work "both ways". The output of the model should be a standardized array, probably along the lines of:

array('2009-08-05' => array('event' => ..., ...), '2009-08-06' => array(...));

I.e. just the pure data.

By default it could spit out the current month, or everything between now and +1 month. It should also take an argument though to change that default behaviour. Either take an array of dates or a range.

I don't know why the first two entries in your example are 'blank'. If your array already represents a month as shown on a calendar sheet, than that would go against MVC. Such presentation issues are to be considered in the View.

deceze