For general purpose CMS it’s not going to be worth it, is it, given the loss of flexibility (TVs become unique to one template)
Here’s one way that it could work:
The modx_site_tmplvars table could still provide a way of specifying general purpose TVs, which could be used in multiple templates. When you go to create a template, you could still select from among the available TVs in that table, but upon doing so, instead of throwing the values in site_tmplvar_contentvalues, it would put the values in a table dedicated to that template. That’s a straightforward change to the base code that doesn’t change what MODx is doing, conceptually, but it does widen the possibilities as far as relationships between types of data because it makes join queries across data categories (courses, instructors, rooms) more straightforward.
If the user updates the general TV in site_tmplvars (changes the data type, name, or other attribute), MODx could find out which tables are using that TV and update the tables accordingly (and/or warn the user that this might truncate data if the field length is being shortened, or change the values if the data type is being changed, etc.).
This wouldn’t solve all of the data relationship scenarios (e.g. it doesn’t create relationship tables for many-to-many or one-to-many relationships), and that would need to be addressed separately, but it simplifies one-to-one relationship queries at least. Here’s a simple hypothetical example of querying all of the course instances for a given semester, combined with the generic information about the courses (not semester-specific):
SELECT *
FROM `modx_site_course_instances` AS i
LEFT JOIN `modx_site_courses` AS c ON i.course_id = c.id
WHERE i.year = '2008' AND i.semester = 'fall'
I like the ease of creating simple queries like that. Is there a similarly streamlined method for creating the equivalent results table in MODx now? As far as I can tell, there isn’t. I could specify each TV one by one as additional LEFT JOINs, or, I don’t know, maybe there’s some other better method (which I would be VERY interested in hearing), but it doesn’t seem as straightforward to me.
... and the redundancy you’ve introduced (potentially multiple TVs of the same datatype per template)?
Having multiple TVs of the same data type in the same template doesn’t seem like an issue at all to me, if what you mean is that there may be 3 fields that are integer types, 6 that are varchar, 2 that are text, etc. Don’t most tables have more than one field of the same data type?