My first Phalcon project is a rebuild of an old procedural database interface, and sometimes I have trouble knowing where in the MVC structure some given functionality should go. I'm currently trying to stick to using Volt for templating, partly because I would otherwise be tempted to do too much coding in the Views.
My current dilemma is special treatment of data fields for display. For example, my Birthdate field is a MySQL date field and therefore must include year, month, and day, but sometimes the year is not known. I use 1900 as my hidden clue that this should be treated as just a birthday with an unknown year, but at what point on the data's travel from Model through Controller to View should I replace the "1900"? I also need to display the age, which is a calculation that is definitely not posssible in Volt even if I was tempted to do it there.
The original suggestion given to me was to add a method called afterFetch() to the Model to trap that event, and do my calculations and put the results in additional properties to $this (each record as it is fetched), which I have been nicknaming "pseudo-fields", because they aren't in the database but sit in the Model as if they are. But I have realized a couple of problems with that:
- The only time that method runs is when my query is a straight
SELECT * FROM thetable ...- if I get just a few fields from the database, the pseudo-fields don't appear, and although I haven't tested a join yet, I bet it doesn't work in that case either.
- I haven't started writing the code for editing data yet, but I suspect that the save() method won't be happy if there are extra fields in the Model that aren't in the database.
So then I consider the Controller, but since it merely passes the array of objects along, I would have to loop through it just for this purpose. Finally, I get back to thinking of the View, because it seems to me the sort of thing that is most closely related to display of the data. But it has too much business logic for Volt.
What do you guys do with this kind of data handling?