【问题标题】:Yii model behaviour in sub class inherits the AR model class子类中的 Yii 模型行为继承 AR 模型类
【发布时间】:2012-10-26 09:13:27
【问题描述】:

我已经实现了一个 crypt 行为类,它可以附加到 AR 模型上,这样,附加的属性将以加密的形式存储并以解密的字符串形式检索。

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,它扩展了User 模型来编写我的自定义函数,这样如果我对我的user 表进行一些更改并重新生成模型,我就不会丢失我自己的函数。

如果我将我的 behavior 函数移动到 MyUser 类,行为不会得到附加并且无法按预期工作

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

任何帮助将不胜感激。 参考链接:Crypt Behavior

【问题讨论】:

  • 我在任何地方都找不到 cryptbehavior 扩展,这是您自己的行为吗?可以粘贴行为的粗略轮廓,它扩展了哪个类,以及它实现/具有哪些功能就足够了。
  • 用参考链接更新了帖子!。感谢您的关注。
  • 没问题,让我看看能不能帮忙
  • 我认为这个博客给了我一些路线图来实现我想要的。 invisipunk.blogspot.in/2011/01/…
  • 你的 yii 是哪个版本的?

标签: activerecord yii behavior


【解决方案1】:

这是有效的解决方案。我需要测试所有场景。感谢@bool.dev 提供的功能。

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

在我的控制器中

$userModel = MyUser::getUserByID(1);

在我看来

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

【讨论】:

    【解决方案2】:

    您还必须将类的static model 函数添加到子类中。就这么多应该可以工作:

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

    【讨论】:

    • 其实你给的博客链接也是对的,但是在yii版本的某个地方,添加了这个功能。我将在此更改发生时更新答案,稍后会更新更多详细信息,至少现在你不应该得到错误的答案。
    • 试图引入一些不触及基本模型类的常见做法。感谢您的答复。我也在寻找数据库组件级别的附加行为,这样我就可以避免这个问题。
    • bool.dev 你住在这里吗?让我回答一些事情! :P
    猜你喜欢
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2017-05-15
    相关资源
    最近更新 更多