Difference between #4 and #5 of
Instantiate a child class that depends a type field from parent model class (CActiveRecord)

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($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;
}
[...]
3 0
4 followers
Viewed: 13 282 times
Version: 1.1
Category: How-tos
Written by: robregonm
Last updated by: robregonm
Created on: Oct 29, 2012
Last updated: 5 years ago
Update Article

Revisions

View all history