简体   繁体   English

获取用户信息 - dektrium / yii2-user Yii2

[英]Get User Profile - dektrium/yii2-user Yii2

I have used dektrium/yii2-user in my application. 我在我的应用程序中使用了dektrium / yii2-user 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. 并且在vendor / dektrium的 User.php中有一个名为getID()的方法,该方法可以通过Yii::$app->user->getID()并返回登录用户的id

However, there is another method named getProfile() whose function is to return complete profile details of currently logged in user. 但是,还有另一种名为getProfile()的方法,其功能是返回当前登录用户的完整配置文件详细信息。 But, this method is giving 500-internal server error. 但是,这种方法给出了500内部服务器错误。

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

I Googled the issue but found nothing... Help me folks.. 我用谷歌搜索了这个问题但没有发现任何事情......帮助我的人们......

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. 因为Yii::$app->user->identity返回当前用户 - User对象。

You are confusing Yii's web user object with the user model :) 您将Yii的Web用户对象与用户模型混淆:)

EDIT: 编辑:

Yii::$app->user is referring to yii\\web\\User - the application component that manages the user authentication status. Yii::$app->user指的是yii\\web\\User - 管理用户身份验证状态的应用程序组件。

You ask that User component to get the 'identity' which is : 您要求User组件获取“身份”,即:

IdentityInterface is the interface that should be implemented by a class providing identity information IdentityInterface是应由提供身份信息的类实现的接口

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. 在这种情况下,Dektrium User模型实现了IdentityInterface ,您可以在其上调用getId并获取User模型的id。

class User extends ActiveRecord implements IdentityInterface

This code: 这段代码:

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

Will return the Profile model data associated with the User 将返回与User关联的Profile模型数据

And you can access it's fields directly: 您可以直接访问它的字段:

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

See dektrium\\user\\models\\Profile for details. 有关详细信息,请参阅dektrium\\user\\models\\Profile


People always gets confused about yii\\web\\User, the IdentityInterface and the User model.. Myself included :p 人们总是对yii \\ web \\ User,IdentityInterface和User模型感到困惑。我自己包括:p

If you have an instance of an user ( $user ), you can use the getProfile () function: 如果您有一个用户实例( $ user ),则可以使用getProfile ()函数:

$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: 如果您没有user的实例,而是id( $ user_id ),则可以直接获取Profile模型的实例:

$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 Yii::$app->user是应用程序中定义的用户模型的接口(在本例中为dektrium用户模型): 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();

试试这个吧

\app\models\User::findOne(Yii::$app->user->identity->id)->profile;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM