I've concocted a script that auto-generates super-convenient DB object classes which allow me to interact with my db tables without writing any SQL at all. They're not perfect, but I like them very much and they save me soooooo much time. Here's an example useage:
// assume $this->db represents my persistence mechanism
// fetches db record with id=42 from table my_user and instantiates an instance of DB_my_user
$record = DB_my_user::fetch_by_pkey($this->db, 42);
// the db object has a property for each db column. These are public and can be changed
$record->first_name = "Herbert";
// updating a db record is this easy
if (!$record->update_db_record()) {
// db update did not work
throw new Exception("There was a problem updating the database!");
}
What I would like to add to these helpful objects is the ability to translate any ENUM values in the db table to multiple languages as conveniently as possible. For example, a db table may have several enum columns. Let's say my user table has a db column status which describes my users:
CREATE TABLE my_user (
// blah blah blah
status ENUM('single', 'married', 'divorced', 'widowed', 'hopeless', 'fugly')
// blah blah blah
);
I would like, as conveniently and elegantly as possible, be able to fetch a user object and refer to its status property while having that status translated into some particular language indicated by the session or by a url parameter. I imagine something like this:
echo $record->status; // outputs the original db value as defined just above. E.g., "divorced"
$record->set_lang("it"); // tells the retrieved object we'd like to translate to some language, in this case Italian
echo $record->translate->status; // outputs "divorziato"
I realize there are several challenges here:
1) Elegant code design using some other class or appropriate magic method such that my IDE code hints a "translate" property to my DB objects. Ideally this is defined in the shared base class common to my DB objects
2) Need scheme that insures good performance. Would like to avoid huge load operation of dozens of language files by implementing a lazy loading procedure. It would be preferable to cache language values on a web server rather than fetching them from a db server every time we need them.
3) Need to determine organization and format of language asset files that map a particular table/field/value/language combination onto the correct value that is as easy to understand and maintain as possible. If the language file organization is awkward, this could introduce quite a burden because we are likely to have a lot of these.
4) Need to decide on dependency injection versus service locator approach for locating/specifying the language translations. If I use the file system to organize my translations, the translation mechanism must at least require a pathname in order to locate the directory where these translations are stored. It would be ideal if we didn't have to toss around some directory location all the time, but using a service locator within my DB class seems wrong because it limits the reusability of that class.
I was imagining a language file organization scheme something like this directory structure in my file system somewhere:
table_name_one
column_one
en.php // contains english value-translation mappings (probably unnecessary)
es.php // contains spanish value-translation mappings
it.php // contains Italian mappings
column_two.php // etc.
en.php
es.php
it.php
table_name_two
...etc...
or something like that. This structure would make it easy to change things if a table name changed. Each DB_* class has its corresponding table name defined in a constant so it would know which top-level folder to look in. Each second-level folder, corresponding to the column name, could be mapped using a magic method or something, and the language would tell you which file to require/include in order to perform the translation of the enum values. One of these files might look something like this:
// my_user/status/it.php
$map = array(
'single' => 'scapolo',
'married' =>'sposato',
'divorced' => 'divorziato',
'widowed' => 'vedova',
'hopeless' =>'disperato',
'fugly' => 'orribile'
);
Any thoughts or input would be greatly appreciated.