I’m doing some work with date/time and getting frustrated (as usual) of how fractured and non-standard this stuff is - and also trailing off into an AR issue.
First off, we have a bunch of types that deal with date/time in Yii: CTimestamp, CDateTimeParser and CDateFormatter - I realize this is an attempt to break down areas of responsibility, but it seems pretty scattered. And it seems there’s quite a bit of duplication in there too - custom implementations of things that PHP (5.3) already does out of the box.
In Yii 2.0, how about extending the built-in DateTime class into CDateTime, and getting rid of all these other helper-classes?
I also would suggest setting the PHP default timezone to UTC - with no option to set it to anything else. This may seem radical, but ideally, nobody should store date/time in databases (at least not MySQL) as anything other than UTC, unless they’re actually using a column-type that stores the timezone, or unless the application serves only a single specific timezone. Any ordinary application that serves North America or Canada needs to support at least two timezones, and if you’re using both date and datetime column-types, you can get yourself into serious trouble.
Which brings me to my next issue: no real support for date/time in AR… Ideally, you should be able to just use DateTime (or CDateTime, if it existed) and just deal with date/time as objects rather than strings - it’s a cruel and unusual stumbling block, and most people resort to using calls like date(‘Y-m-d H:i:s’, $mytime) which unfortunately not only formats things in an SQL-compatible format, but also converts the time to the PHP default timezone; many users do not understand this.
Regardless of whether you agree with that particular point of view or not, it’s clear there’s something missing as far as handling datatypes in general in AR - not just datetime, but certain other types could be handled better as well: booleans, enums, flags, etc. have no proper support in terms of O/R mapping.
Poking through the AR codebase, looking for a way to add support for the DateTime type, I’m thinking there’s a bigger conceptual issue with AR in general: the one-to-one mapping and coupling between attributes and columns. This precludes mapping anything other than simple value-types directly to database, e.g. user-defined types - for example, in a customer database, a common requirement is to have an Address type, and mapping two instances (e.g. mailing and billing addresses) to a customer-record. Another example would be an Audit type (e.g. created, created_by, updated, updated_by) which could be added as a single attribute to any model-type, rather than having to define the 4 attributes individually on each type and applying a behavior.
My immediate train of thought is along the lines of an O/R service-component (attached to Yii::app) which would define default mapper-types for attribute-types… of course, to implement something like that, you would need to define attribute-types which PHP doesn’t currently support, so this might involve yet another callback on CActiveRecord that defines the attribute-types - unless you’re willing to consider adding support for metadata in general, e.g. via doc-blocks, which just brings my trail even further from where I started:
If you had attribute-types defined as metadata (regardless of whether it’s provided by a callback or doc-blocks) it would seem natural to use the same information for other things, e.g. making it a general feature of CModel, so that CFormModel and eventually HTML-helpers could use the type-information to establish various defaults…
Which would then lead me think about having a general metadata-facility, so we could define even more metadata that would be useful to HTML-helpers for establishing even more defaults, and so we didn’t need a growing number of callbacks for various types of metadata…
And so on along those lines…
I guess in my mind perhaps I’m turning Yii into something entirely different than what it is
Anyway, these are just some thoughts that bumped around in my head this afternoon and I thought I’d share…