Instantiate a child class that depends a type field from parent model class (CActiveRecord)

Comparing #4 with #5

Revision #5 was created by robregonm robregonm on Oct 30, 2012, 1:45:02 PM.

Fixing small typo in condition

Content

[...]
{
if (!isset($attributes['type_id'])) { // When creating and $type_id has not defined yet
return parent::instantiate($attributes);
}
$classRecord = AnimalType::model()->findByPk($attributes['type_id']); // Load the "Type" class
if($class
Record === null)
throw new CException('Type not found');
$className = $classRecord->name; // Let's assume that name is the field storing the name of the class
$model = new $className(null); // Null param needed for populating the record
return $model;
}
[...]