3

I have used dektrium/yii2-user in my application. And there is a method named getID() in User.php of vendor/dektrium and this method can be accessed by Yii::$app->user->getID() and returns id of the logged in user.

However, there is another method named getProfile() whose function is to return complete profile details of currently logged in user. But, this method is giving 500-internal server error.

exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\web\User::getProfile()' in ... ...

I Googled the issue but found nothing... Help me folks..

Choxx
  • 945
  • 1
  • 24
  • 46

3 Answers3

6

I believe that you can get the profile of the currently logged in user like this:

Yii::$app->user->identity->profile;

because Yii::$app->user->identity returns the current user - the User object.

You are confusing Yii's web user object with the user model :)

EDIT:

Yii::$app->user is referring to yii\web\User - the application component that manages the user authentication status.

You ask that User component to get the 'identity' which is :

IdentityInterface is the interface that should be implemented by a class providing identity information

In this case, Dektrium User model implements the IdentityInterface and you are able to call getId on it and get the id for the User model.

class User extends ActiveRecord implements IdentityInterface

This code:

Yii::$app->user->identity->profile;

Will return the Profile model data associated with the User

And you can access it's fields directly:

Yii::$app->user->identity->profile->location;

See dektrium\user\models\Profile for details.


People always gets confused about yii\web\User, the IdentityInterface and the User model.. Myself included :p

jacmoe
  • 1,637
  • 13
  • 17
  • Hey hey hey! I'm unable to figure out what data is coming out using `Yii::$app->user->identity->profile;`. However I figured out that the issue with my code is: I am trying to access the method present in dektrium's User.php and by default, the code `Yii::$app->user->getProfile;` is searching for method in **yii\web\User** not in **dektrium/../User** – Choxx Feb 12 '16 at 08:42
  • Updated post to explain how to get the profile data – jacmoe Feb 12 '16 at 11:59
1

If you have an instance of an user ($user), you can use the getProfile() function:

$profile = $user->getProfile()->one();

And it returns profile record from that user.

If you don't have an instance of user, but the id ($user_id), you could get an instance of Profile model directly:

$profile = Profile::find()->where(['user_id' => $user_id)->one();

And Yii::$app->user is an interface to the user model defined in your app (dektrium user model in this case): http://www.yiiframework.com/doc-2.0/yii-web-user.html

to sum up:

$user_id = Yii::$app->user->getID();
$profile = Profile::find()->where(['user_id' => $user_id)->one();
  • No, `Yii::$app->user`is not an interface to the user model, but it can get the currently logged in user by using `identity` – jacmoe Feb 12 '16 at 06:10
  • public function getId() { $identity = $this->getIdentity(); return $identity !== null ? $identity->getId() : null; } – jacmoe Feb 12 '16 at 06:10
  • @stig-js the approach you described is not efficient. Instead of this currently I'm using `$profile = Profile::findOne(Yii::$app->user->getID());` which is I think better one than you gave... – Choxx Feb 12 '16 at 08:15
  • 1
    @choxx findOne uses internally findbycondition that also calls find method: http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#findOne%28%29-detail https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L100 –  Feb 12 '16 at 12:59
  • 1
    @jacmoe you're right, as it's said in the link i've posted, Yii::$app->user is the class for the "user" application component that manages the user authentication status. So, in config you've defined user class to be dektrium one, but i've simplified too much. –  Feb 12 '16 at 13:02
  • @stig-js no wonder people are having a hard time understanding all of this :) I just wanted to point out that User and User is not the same, and that there is an IdentityInterface between these two. ;) – jacmoe Feb 12 '16 at 13:16
  • @stig-js yeah that's true... I agree since you posted proof :-p But instead of writing a long line, I'd prefer writing short code. I'm lazy. That's my problem... ;) – Choxx Feb 15 '16 at 10:56
  • @choxx don't worry ;) –  Feb 15 '16 at 11:47
0

Try this one

\app\models\User::findOne(Yii::$app->user->identity->id)->profile;
makDee
  • 1
  • 1
    It would be better to explain why this would would work to let him understand why his formula does not. – phaberest Feb 11 '16 at 20:10