2

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)

1
  • pls, fix return $this->_user->account; to return $this->_user;
    – Dimmduh
    Feb 4, 2015 at 17:30

3 Answers 3

6

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

3

Similar to Afnan's answer, but just my 2 cents. I extend the webuser class like Afnan suggests but also have an account model which I use for the database access and model rules. I access this by having a function on my ExWebUser class:

public function getAccount(){
    return Account::model()->findByPk($this->id);
}

This allows me to access it with Yii::app()->user->account->name which feels much cleaner than having a method per action if you have a lot of things to do.

P.S
This can obviously be much improved by caching the result in a private variable on the extended class and possibly pre-loading on the init() method.

1
  • 2
    Preloading in Init isn't possible in this scenario since upon init there is no user logged in, and I needed the relational data of the logged in user. Thnx for your answer anyhow, i think my current solution is quite neat.
    – TheWolfNL
    Nov 4, 2012 at 22:56
0

Thanks for the help ! I just noticed that i had to change your code to :

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;
    } else {
        return false;
    }

  }
}

I only return $this->_user to access my User's attributes like Yii::app()->user->account->name

1
  • By changing the function name to getInfo() you should be able to use Yii::app()->user->info->name In case you dislike using "account".
    – TheWolfNL
    Mar 15, 2013 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.