Models hierarchy?

I’m trying to come up with some clever solution for a common problem - models inheritance. It’s like to map a standard OOP inheritance principle to a MVC paradigm, so to speak.

Let’s assume that there’s a need to develop a system to keep vehicles registry. So a base class should have fields ‘name’ and ‘weight’. In terms of SQL, it uses a table ‘vehicle’:




create table vehicle (

vehicle_id serial primary key,

name text,

weight integer,

type_id integer);



‘type_id’ is just a FK to a simple ‘types’ table, holding values like ‘car’, ‘bicycle’, ‘truck’ etc.

A couple of children - one for car, has additional field ‘horse_power’. SQL:




create table vehicle_car (

vehicle_id integer primary key,

horse_power integer);



another - for bicycles with additional field ‘gears’, like so:




create table vehicle_bicycle (

vehicle_id integer primary key,

gears integer);



Obviously, one wants to keep common info (here, name and weight) in the base table while type-specific info in one of the children tables.

(as a side remark, PostgreSQL has rudimentary support for this type of table relationships, just add ‘inherits (vehicle)’ at the end of children tables definition).

Moving forward, how this stuff will fit into Yii’s MVC?

Did some quick search, found stuff like How to use a single form to collect data for two or more models. It’s not bad, except the logic is entirely in a controller’s side. My understanding is that the model should be fully aware of how it is mapped into the DB.

Other related question is how I construct a form to enter, say, a bicycle details? It has to be partial forms, right? Have no idea how to implement them, though.

Third question - a list of all vehicles regardless of their type. I know how to construct a view to display any given type (by model). But how to do it dynamically, like in Drupal views? There each node type might have its own ‘render’ function so as a programmer I just have to select needed node ids by some criteria and just call this ‘render’ in a loop for each (this is very simplified explanation).

Any good ideas?

STI = Single Table Inheritance.

http://www.yiiframework.com/wiki/198/single-table-inheritance/

There is also something called CTI, Class Table Inheritance but I haven’t gone there yet.

Also check this blog post for some other ideas.

http://sebgoo.blogspot.com/2012/02/yii-class-table-inheritance.html