duanbarong4321 2012-11-03 14:57
浏览 106
已采纳

Yii从Yii :: app-> user访问关系数据

I would like to know whether it is possible or if there is a easier way to do this.

On many pages i'd like to use the relational data, now every page i use it i place the following code:

<?php
    if(!Yii::app()->user->isGuest){
        $user = User::model()->findByPk(Yii::app()->user->id);
    }
?>

So I can use the following in the view:

$user->account->name

To display current users related account's name

It would be nice if I could just do the following:

Yii::app->user->account->name

Set state isn't what i'm looking for since it's not only the account name i want to use, and I don't want to add a state for all relational data I might want to use.

 Solution

 |
 |
 |
 V

Solution

I combined the good of both answers to create the following:

class WebUser extends CWebUser {
private $_user;

public function getAccount(){
    if(!$this->isGuest && (!isset($this->_user) || Yii::app()->user->id != $this->_user->id)){
        $this->_user = User::model()->findByPk(Yii::app()->user->id);
    }
    if(!empty($this->_user)){
        return $this->_user->account;
    } else {
        return false;
    }

  }
}

So now I can use the following:

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

The above method retrieves the data if the user is logged in and there is no data stored or the user has a different id.

Place it in Components and be sure to add the part Afnan talks about to the config (main.php)

I accept the answer of Afnan because it helped me most (The part I needed to know)

  • 写回答

3条回答 默认 最新

  • duanke6057 2012-11-03 19:53
    关注

    Well i do not know if it is the best solution but i usually do things like this by extending CWebUser

    What i would do is create file in Components folder that extends CWebUser

    class CWebUserExtend extends CWebUser
    {
        /**
         *
         * @return the type of user based on forien key of UserType
         */
        public function getUserAccname()
        {
            $user = User::model()->findByPk(yii::app()->user->id);
            if ($user === null)
                return 'error';            
            else
                return $user->account->name;            
        }
    }
    

    and in main.php file modify 'User' index in following manner

    'user'=>array(
                // enable cookie-based authentication
                'allowAutoLogin'=>true,
                            'class'=>'CWebUserExtend',//class that extends CWebUser
            ),
    

    This would allow you to do something

    Yii::app()->user->getUserAccname();
    

    Hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 模拟电路求复阻抗和传递函数,请各位拍照写一下解答过程
  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio