Yii model behaviour in sub class inherits the AR m

2019-07-23 14:37发布

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

2条回答
Rolldiameter
2楼-- · 2019-07-23 15:04

You'll also have to add the static model function of the class to the subclass. Just this much should work:

public static function model($className=__CLASS__)
{
    return parent::model($className);
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-23 15:05

Here is the working solution. I need to test all the scenarios. Thanks to @bool.dev for your function.

class MyUser extends User 
{
    public static function model($className=__CLASS__)
    {
       return parent::model($className);
    }

    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 static function getUserByID($id)
   {
      //validation of $id goes here..

      return MyUser::model()->findByPk($id);
   }
}

in my controller

$userModel = MyUser::getUserByID(1);

in my view

$userModel->password; //gives me the decrypted password; for easy understanding, i used password field here....
查看更多
登录 后发表回答