【问题标题】:How bind CWebUser with model User如何将 CWebUser 与模型用户绑定
【发布时间】:2013-06-20 07:12:14
【问题描述】:

如何将 Yii:app()->user 绑定到模型 User。有一个连接类型:Yii::app()->user->getUser()

比如我想获取当前登录用户的邮箱:

Yii::app()->user->getUser()->email;

我阅读了 wiki 帖子:http://www.yiiframework.com/wiki/80/add-information-to-yii-app-user-by-extending-cwebuser-better-version/

【问题讨论】:

    标签: php authentication yii yii-components


    【解决方案1】:

    组件中的MyWbuser.php:

    class MyWebUser extends CWebUser{
    
        private $_profile = null;
        public $loginUrl='/';
    
        public function init(){
           parent::init();
           if(!$this->getIsGuest()){
                $this->_profile = User::model()->findByPk($this->getId());
           }
        }
        public function getProfile(){
           return $this->_profile;
       }
    }
    

    在 config.php 中:

        'user' => array(
            'class' => 'MyWebUser',
        )
    

    那么你可以使用:

    Yii::app()->user->profile->name
    

    【讨论】:

    • 这是个好主意)据我所知,而不是 UserIdentity?
    • 是的,它扩展了 CWebUser
    【解决方案2】:

    尝试将此代码添加到您的用户身份验证功能中

    $this->setState('email',$this->email);
    

    如图所示,用于在任何地方访问该电子邮件

    Yii::app()->user->getState('email');
    

    【讨论】:

    • 这是个好主意。谢谢你,尼纳德)顺便说一句,昨天的任务我应付了=)@尼纳德
    • 干杯 :) 有什么问题??
    • 我使用的是模型 User 而不是 RegisterForm )
    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 2015-11-10
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多