AR class hierarchy

Currently I am trying to created a small hierarchy of AR models in order to allow different rule sets depending on certain classes of data. The goal of this is to allow for fields that would not be visible in certain cases to be required when they are in-fact visible.

For example say I have screens for domestic and wild animals that both share the same table in a database. On the domestic page is a field for a collar size and on the wild animals page there is a dropDownList for the animals natural habitat. On these separate screens I would like these fields to be "required". If they share the same AR model then adding the collar-size as required in the rules section of the AR model will cause a problem when submitting data on the wild animals page as it does not exist (and thus is always absent upon submission). For this reason I want to create a AR class that houses the bulk of the model info with 2 subclasses that inherit from this class and handle only the rules for that are specific to the different pages. So, there would exist a class called Animals, then 2 subclasses called Domestic and Wild that extend Animals, which in turn extends the ActiveRecord class.

This works fine when creating new records, however an issue arises when trying to update information. The problem seem to come from the call to the static ‘model’ method to retrieve populate existing data from the database. When I use this method the class that is returned is always the parent class.

As per the documentation the ‘model’ method is the same for both the parent and child classes.





public static function model($className=__CLASS__)

{

 return parent::model($className);

}



I did some debugging and it does appear that whenever the "model" method is used the parent class is always created, even if I explicitly use the child class. So,

Animals::findByPk,

Wild::findByPk,

and

Domestic::findByPk

all return a Animals class. I must be missing something (I’m thinking I may misunderstand how the static model method works). If anyone has any advice on a better way of doing this or why this isn’t working I’d really appreciate it.

It’s important to remember adding the model() function to the child AR class. Otherwise, if you call find…() on the class instance (which you should), you will get an object of the parent class.

Thus




public static function model($className=__CLASS__)

{

  return parent::model($className);

}



is mandatory for


Child::model()->find...

to work as expected.

/Tommy