Revision #5 has been created by robregonm on Oct 30, 2012, 1:45:02 PM with the memo:
Fixing small typo in condition
« previous (#4)
Changes
Title
unchanged
Instantiate a child class that depends a type field from parent model class (CActiveRecord)
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
activerecord, CActiveRecord, factory, instance of child model, active
Content
changed
[...]
{
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($classRecord === 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;
}[...]