Is it OK to add an attribute to an ActiveRecord model if that attribute does not correspond to a field in the database?
Is it OK to add an attribute to an ActiveRecord model if that attribute does not correspond to a field in the database?
I am going to answer my own question. Yes, it’s all right to add an attribute to AR even if it does not correspond to a database field. It is called a “virtual attribute” and it is declared in the model class as follows:
public helperVariable;
It can be accessed in the following way:
$model->helperVariable
More info:
http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/
If that attribute is new column then what to do?.i have that problem.please help me regarding my issue.
you dont need to declare any variables explicitly since AR recognize it automatically. so you can use your new colum attribute without any problem like this
echo $model->new_column_attribute;
if you want to customize labels and validation rules you can provide it on its corresponding model