samdark, your post comes right in time for me. I just was thinking about how to work with inheritance between AR classes and like your approach very much. Thanks for sharing!
Sorry, for taking over this thread a little, but it’s probably related:
Any suggestion how to work with different sets of attributes for the different sub types?
For example, take the following attributes:
Shared by all car types: name, power, max_speed, …
Family cars: number_of_seats, …
Sport cars: power_to_wind_ratio, …
I thought about hardcoding the additional attributes in the type classes and store their values in another table car_attributes like this:
id (INT) - id of attribute
car_id (INT) - id of car (parent)
name (VARCHAR) - name of attribute ('number_of_seats', 'power_to_wind_ratio')
datatype (TINYINT) - id of datatype (1=string, 2=int, 3=float, ...)
value_string (VARCHAR) - value for datatype string
value_int (INT) - value for datatype int
value_float (DECIMAL) - value for datatype decimal
Each dynamic attribute value would be represented by a row in this table. The column ‘datatype’ stores the type of data and there’s one data column for every possible datatype (value_string, value_int, value_float, etc). Using a HAS_MANY relation in Cars with the ‘index’ feature would allow easy access to the attribute records:
'dynamics'=>array(self::HAS_MANY, 'CarAttributes', 'car_id','index'=>'name'),
$familycar=FamilyCar::model()->with('dynamics')->findByPk($sportcar_id);
// safety checks like isset(...) left out for clarity...
$seatNumber=$familycar->dynamics['number_of_seats']->value_int;
But as you see this makes it more complicated to access the value of such a dynamic attribute. And the "one-column-per-type" approach is not very ressource friendly. Not to mention that there are more types than i gave in the example.
Ideas? Alternative approaches?