Not save attribute in model


Let say I don’t want to save an attribute (like the created date) in a model, how can I do that the Yii way ?

unset($this->attribute) //?

Now, let say I want to save the created attribute, but I need to format it before displaying it. In the afterFind method I use the dateFormatter to display the timestamp in a view. If this view is a form, and I validate it, the script in the controller goes this way :

  1. Retrieve the model with findByPK (here, the afterFind method is called and the created attribute is overrided with the friendly format)

  2. Massive assignement (the created attribute is not assign cause not in the validated form)

  3. Validation (let say it goes fine)

  4. before save, change back the created attribute to a timestamp

  5. save

As you can see, when I retrieve the model, the created attribute is automatically changed to a friendly format. So I have to change it back to a timestamp format before save.

It works, but he feels wrong to me :confused:

Can I override the magic __get() function to retrieve the created attribute as a friendly format ? Since the created attribute is never changed in the model, I don’t have to mess with afterFind and beforeSave.

What do you think ?

Thanks for helping me :)


Just to let you know, what I am looking for in my last question is a way to use the transient property just like in java :)

If it is impossible, I have these choices :

  1. override __get() method

  2. change attributes in the afterFind and change them back in the beforeSave

  3. create some new attributes (like formattedAttribute) in which I store the formatted values

What do you think ?

my 3 cents:

  1. override -----NO

  2. change in afterFind/beforeSave ------hmmmmmm, well

  3. virtual attribute ---- yes, if for date field, i would add extra parameter for format yyyy-mm-dd or mm-dd-yyyy

Thanks for you reply !

Right now, I use the virtual attributes. I tried the afterSave/beforeSave methods, but it feels a lot of code for almost nothing.

I have to say, it is not just for the created attribute, but also for the image attribute.

My model return the image name (like 12345.jpg). And I want the model to return the whole path (like /assets/img/12345.jpg) or if there is no image, a default one (like /assets/img/default.jpg).

I don’t want to store the path in the database (since it can change), and I need to set a default image. To finish, I can’t imagine a test in every view like this one :

$model->avatar === 'default.jpg' ? <img src="$imgUrl.$model->avatar" /> : <img src="avatarUrl.$model->avatar" />;

Not dry ! It seems normal to me that the model return the whole path so I don’t have to write it in every view. After all this is datas coming from database, and the path is a part of this data (even if not in the database).

For now, i will stick to the virtual attributes, but if you have any better idea :)

no, i don’t have a better idea.

virtual attribute is the way to go for me, all dirty and fancy data manipulation handled by model.


agree :)