MySQL Expression as a regular field in CActiveRecord

How can i configure a CActiveRecord model so that instead of "my_attribute" it will select "UNHEX(my_attribute)" ?

Similary i will need Yii to save the model using "HEX(my_attribute)". For the second part i was thinking of using "beforeSave". Is there also another approach ?

Thanks!

Usually this kind of work are done, as yuo correctly noticed, in the function beforeSave and afterSave.

You can write this function and use the php equivalent function for accomplish the purpose.

If you want to select from database, you should configure the property ‘select’ of CDbCriteria:




$criteria= new CDbCriteria;

$criteria->select= '...all your fields..., UNHEX(my_attribute)as my_attribute';

Mymodel::model()->findAll($criteria);



nice, thanks!