Changes
Title
unchanged
Extending an ActiveRecord model
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
active record, CActiveRecord, extending
Content
changed
[...]
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
*
Overrides the ActiveRecord save to insert the extra parameters
* This class has into JSON text and save it in the data baseSaves meta data in the params attribute
* @return boolean
*/
public function
save($runValidation = true, $attributes = nullbeforeSave() {
$this->name = get_class($this);
$vars = get_object_vars($this);[...]
}
/**
*
Overrides the ActiveRecord function to read the extra params andCreates the correct class type and populates the meta data
*
create a new class based on the data and return the sub class@param array $attributes
* @return CActiveRecord
*/
public function
fin
dByPk($pk, $condition = '', $params = array()) {
$result = parent::findByPk($pk, $condition, $params);
if ($result !== null) {
$extendedVstantiate($attributes) {
$vars = json_decode($
result->attributes['params
'], true);
$type = $
extendedVvars['name'];
$subClass = new $type;
foreach ($
extendedVvars as $key => $var) {
if (property_exists($subClass, $key)) {
$subClass->$key = $var;
}
}
$subClass->attributes = $result->attributes;
return $subClass;
}
return falsereturn $subClass;
}
}
```
Now all we need to do is change the subclass definitions to extend from this new class[...]