I have implemented a crypt
behavior class that can be attached to a AR model so that, attached attributes will be stored as encrypted and retrieved as decrypted string.
class User extends CActiveRecord
{
public function behaviors()
{
return array(
'crypt' => array(
// this assumes that the behavior is in the folder: protected/behaviors/
'class' => 'application.behaviors.CryptBehavior',
// this sets that the attributes to be encrypted/decrypted are encryptedfieldname of the model
'attributes' => array('password'),
'useAESMySql' => true
)
);
}
}
This is working fine. I am also having my custom class Myuser
which extends User
model to write my custom functions so that if i make some change in my user
table and regenerate model, i wont loose my own functions.
If i move my behavior
function to the class MyUser
, the behavior is not getting attached and not working as expected.
class MyUser extends User
{
public function behaviors()
{
return array(
'crypt' => array(
// this assumes that the behavior is in the folder: protected/behaviors/
'class' => 'application.behaviors.CryptBehavior',
// this sets that the attributes to be encrypted/decrypted are encryptedfieldname of the model
'attributes' => array('password'),
'useAESMySql' => true
)
);
}
public function customfn1()
{
//some code goes here...
}
}
Any help would be appreciated. Reference Link: Crypt Behavior
You'll also have to add the
static model
function of the class to the subclass. Just this much should work:Here is the working solution. I need to test all the scenarios. Thanks to @bool.dev for your function.
in my controller
in my view