0

I have a user_detail table

CREATE TABLE `user_detail` (
  `id` int(11) unsigned NOT NULL,
  `userID` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
);

I want to retrieve the value of user_detail.name in main.php to display the loggedin user's name.

in UserDelail class I added a getter:

public function getFullName()
    {
        return $this->hasOne(UserDetail::className(), ['userID' => Yii::$app->user->identity->id]);
    }

In main.php I have <?= UserDetail::getFullName()->name ?>

and I am getting: Calling unknown method: yii\web\View::hasOne()

What is wrong here? and how can i correct that?

Thank you

Michel
  • 1
  • 2

3 Answers3

1

edit.

To get current logged user data u can simply:

Yii::$app->user->identity->id
Yii::$app->user->identity->name

And u can do it wherever: directly in the view, in controller, model and so on...


Why u are making realtion to ClassX in ClassX.

The whole idea of realtions is to connect ClassX to ClassY.

You should add in for example model/Project:

 public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'userId']);
    }

And the and in the model/User:

public function getProject() {
        return $this->hasMany(Project::className(), ['userId' => 'id' ] );
    }

Now when u want to throw out somedata from User table in Project/index for example you just (in model/project):

public function getUserName() {
        return $this->user ? $this->user->username : '- no user type-';
    }

Next in the project view/projectIndex:

'<?= GridView::widget([
'dataProvider' => $dataProvider,        
 'filterModel' => $searchModel,       
        'columns' => [
'userName',
],]);

Or:

   <?php $new = new Project; ?>
   <?= $new->getUserName(); ?>

It all depends where and what for, you want to throw data.

fsn
  • 549
  • 3
  • 11
  • Thank you for your answer. However I cannot do Yii::$app->user->identity->name because the details of the user is not found in User. the details are in user_detail. – Michel Jul 30 '15 at 17:38
  • so just create function getUserName in your model and exract it from your user_detail by id. – fsn Jul 30 '15 at 17:40
  • @Michel, you want some clear example or now you know to do it? – fsn Jul 30 '15 at 17:57
  • I appreciate all help Indeed this is what I did; I created a model for user_detail (UserDetail) where the name is stored and I created a getter (getFullName)[see details in my first post]. but it is not working, there is something missing that I cannot get my hands on so please any help is much appreciated – Michel Jul 30 '15 at 18:34
0

You haven't address the namepsace correctly in either your View file or your Model class

Please add something like use app\models\UserDetail at the top of your view file.

chaintng
  • 1,325
  • 3
  • 14
  • 26
0

I was able to solve it in the following way:

   public function getFullName()
    {
        $userInfo = UserDetail::findOne(['userID' => Yii::$app->user->identity->id]);
        return $userInfo->first . ' ' . substr($userInfo->middle, 0,1) . '. ' . $userInfo->last;
    }

I do not like this solution but it is what I have for now in wait for a better solution.

Michel
  • 1
  • 2