Get/set Attributes - Virtual Attributes

Suppose in my Listing model I have:


public $categories;

and in my ListingController I do:


$model->attributes = unserialize($other_model->categories);

Now when I do:


print_r($model->attributes);

it does not include ‘categories’.

Any reason why?

Make sure you have a rule set for that attribute in your rules() method.

I have. Just to be extra sure I even set a ‘safe’ rule for the attribute.

Should you not use this?




$model->categories = $other_model->categories;



I’m not quite sure of the relationship between your two models.

I use unserialize because I’m storing this data as a serialized array.

Either way I suppose the question is, does $model->attributes include virtual attributes?

I believe so, as long as the attribute has rules defined for the current scenario.

I do not think so ?

CModel:


public function setAttributes($values,$safeOnly=true){

    if(!is_array($values))

        return;

    $attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());

...

CActiveRecord:


        public function attributeNames()

        {

                return array_keys($this->getMetaData()->columns);

        }

Unless you are explicitly setting only safe attributes, the selected attributes are those from the database table.