I have a model class with some basic values, and now I want to extend it with a calculated ID field. In my system we use an ID for every entity, that is containing the type of the entity and the auto-increment id from the DB.
I would need a parameter, call it now $cid (calculated id) that is setted when it initialized.
I've tried to set it in the init/model functions, but I get Property "Product.cid" is not defined. Exception.
And I've tried to create a function:
public function _cid($value = null) {
if($value == null){
return $this->cid;
}else{
$this->cid = $value;
return $this->cid;
}
}
How should I extend my model to have this value as a parameter of the Model?
Update
Jon answered really well and the official docs are really helpful. But, with this solution, now the getCid function is only called, when I call it independently. When I call it via the model's getAttributes($model->safeAttributeNames) (or getAttributes(array('cid'))), I get null as the value of $model->cid and the getCid method is not called. (attribute is setted to be safe)