Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:38:40+00:00 2026-06-13T17:38:40+00:00

I would like to know whether it is possible or if there is a

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:38:41+00:00Added an answer on June 13, 2026 at 5:38 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know whether this is actually possible in Android? When Android
I would like to know whether it is possible to use a SELECT statement
I dont know whether this is possible but it would be great if there
I would like to do the following. I don't know whether it's possible to
I would like to know whether it is possible to define own doxygen comment
I just shoot myself in the foot and would like to know whether there
I would like to know whether it is possible to let PHPmyadmin give back
I would like to know whether or not it is possible to clear an
I would like to know whether it is possible to examine the contents of
Accuracy Vs. Precision What I would like to know is whether I should use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.