在子类的Yii模型行为继承了AR模型类 [英] Yii model behaviour in sub class inherits the AR model class

查看:185
本文介绍了在子类的Yii模型行为继承了AR模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个地穴行为类,可以连接到AR模型,这样,附加属性将被保存为加密和检索作为解密的字符串。

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
           )
        );
    }
}

这是工作的罚款。我也有我的自定义类 MYUSER 延伸用户模式来写我的自定义功能,因此,如果我做出一些改变在我的用户表并重新生成模型,我不会失​​去我自己的功能。

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.

如果我将我的的行为函数的类 MYUSER 的行为是没有得到连接并未正常工作

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...
    }
}

任何帮助将是AP preciated。 参考链接:<一href="http://www.yiiframework.com/forum/index.php/topic/26435-aes-encryption/page__view__findpost__p__127764"相对=nofollow>地穴行为

Any help would be appreciated. Reference Link: Crypt Behavior

推荐答案

下面是可行的解决方案。我需要测试所有的场景。感谢@ bool.dev为你的函数。

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);

在我看来

$userModel->password; //gives me the decrypted password; for easy understanding, i used password field here....

这篇关于在子类的Yii模型行为继承了AR模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆