I’ve created a behavior for handling group of properties for a given model (such as abilities or preferences for User), usually represented by a long list of checkboxes.
The way I’ve done this is using single table inheritance, with a database table called ItemProperties, and then a class structure of e.g.
abstract class ItemProperty{
/*Single table inheritance parent class */
function getAvailableProperties();
function getProperty();
function setProperty();
}
class UserPreference extends ItemProperty();
class UserAbility extends ItemProperty();
(I’ve used child classes because they all have a list of different available properties)
Each instance of ItemProperty represents a single property (e.g. a checkbox)
I then use behaviors attached to the Item to handle groups of Properties.
E.g.
class ItemPropertyGroup extends CBehavior
{
/*the class the property applies to*/
public $propertyClass;
function getPropertyList()
{
/*gets a property list ready for creating a checkbox*/
}
}
class PreferencesGroup extends ItemPropertyGroup
{
public $propertyClass = 'UserPreference'
public function getPreferenceList()
{
return $this->getPropertyList();
}
}
class AbilitiesGroup extends ItemPropertyGroup
{
public $propertyClass = 'UserAbility'
public function getAbilityList()
{
return $this->getPropertyList();
}
}
and then in my User class (for example), adding
class User extends CActiveRecord
{
behaviors()
{
return array (
'UserPreferences' => array (
'class'=> 'ext.PreferencesGroup'),
'UserAbilities' => array (
'class'=> 'ext.AbilitiesGroup'),
)
);
);
}
}
Which works fine, except every time I create a new set of properties, I have to create a new child class of ItemPropertyGroup with alias functions. The reason I did this was because as far as I’m aware, if I just use the same class and attach it several times over to my User model, the getPropertyList() function will just overwrite itself.
Is this correct? Can you recommend a more elegant way to achieve the same outcome?