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 ?
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 :
Retrieve the model with findByPK (here, the afterFind method is called and the created attribute is overrided with the friendly format)
Massive assignement (the created attribute is not assign cause not in the validated form)
Validation (let say it goes fine)
before save, change back the created attribute to a timestamp
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
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.
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