获取用户配置文件 - dektrium/yii2-user Yii2
Get User Profile - dektrium/yii2-user Yii2
我在申请中使用了 dektrium/yii2-user。
vendor/dektrium的User.php中有一个名为getID()
的方法,可以通过已登录用户的 Yii::$app->user->getID()
和 returns id
。
然而,还有另一种名为 getProfile()
的方法,其功能是 return 完成当前登录用户的个人资料详细信息。但是,此方法会出现 500 内部服务器错误。
exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\web\User::getProfile()' in ... ...
我用 Google 搜索了这个问题,但一无所获...帮助我的人..
我相信您可以像这样获取当前登录用户的配置文件:
Yii::$app->user->identity->profile;
因为 Yii::$app->user->identity
return 是当前用户 - User
对象。
你混淆了 Yii 的网络用户对象和用户模型:)
编辑:
Yii::$app->user
是指 yii\web\User
- 管理用户身份验证状态的应用程序组件。
您要求该用户组件获取 'identity' 即:
IdentityInterface is the interface that should be implemented by a class providing identity information
在这种情况下,Dektrium 用户模型实现了 IdentityInterface
,您可以对其调用 getId
并获取用户模型的 ID。
class User extends ActiveRecord implements IdentityInterface
此代码:
Yii::$app->user->identity->profile;
将return的Profile
模型数据关联到User
您可以直接访问它的字段:
Yii::$app->user->identity->profile->location;
详情见dektrium\user\models\Profile
。
人们总是对 yii\web\User、IdentityInterface 和 User 模型感到困惑。
包括我自己 :p
试试这个
\app\models\User::findOne(Yii::$app->user->identity->id)->profile;
如果你有一个用户实例 ($user),你可以使用 getProfile() 函数:
$profile = $user->getProfile()->one();
它 returns 来自该用户的个人资料记录。
如果你没有user实例,但有id($user_id),你可以直接获取Profile模型实例:
$profile = Profile::find()->where(['user_id' => $user_id)->one();
并且 Yii::$app->user
是您应用中定义的用户模型的接口(在本例中为 dektrium 用户模型):http://www.yiiframework.com/doc-2.0/yii-web-user.html
总结一下:
$user_id = Yii::$app->user->getID();
$profile = Profile::find()->where(['user_id' => $user_id)->one();
我在申请中使用了 dektrium/yii2-user。
vendor/dektrium的User.php中有一个名为getID()
的方法,可以通过已登录用户的 Yii::$app->user->getID()
和 returns id
。
然而,还有另一种名为 getProfile()
的方法,其功能是 return 完成当前登录用户的个人资料详细信息。但是,此方法会出现 500 内部服务器错误。
exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\web\User::getProfile()' in ... ...
我用 Google 搜索了这个问题,但一无所获...帮助我的人..
我相信您可以像这样获取当前登录用户的配置文件:
Yii::$app->user->identity->profile;
因为 Yii::$app->user->identity
return 是当前用户 - User
对象。
你混淆了 Yii 的网络用户对象和用户模型:)
编辑:
Yii::$app->user
是指 yii\web\User
- 管理用户身份验证状态的应用程序组件。
您要求该用户组件获取 'identity' 即:
IdentityInterface is the interface that should be implemented by a class providing identity information
在这种情况下,Dektrium 用户模型实现了 IdentityInterface
,您可以对其调用 getId
并获取用户模型的 ID。
class User extends ActiveRecord implements IdentityInterface
此代码:
Yii::$app->user->identity->profile;
将return的Profile
模型数据关联到User
您可以直接访问它的字段:
Yii::$app->user->identity->profile->location;
详情见dektrium\user\models\Profile
。
人们总是对 yii\web\User、IdentityInterface 和 User 模型感到困惑。 包括我自己 :p
试试这个
\app\models\User::findOne(Yii::$app->user->identity->id)->profile;
如果你有一个用户实例 ($user),你可以使用 getProfile() 函数:
$profile = $user->getProfile()->one();
它 returns 来自该用户的个人资料记录。
如果你没有user实例,但有id($user_id),你可以直接获取Profile模型实例:
$profile = Profile::find()->where(['user_id' => $user_id)->one();
并且 Yii::$app->user
是您应用中定义的用户模型的接口(在本例中为 dektrium 用户模型):http://www.yiiframework.com/doc-2.0/yii-web-user.html
总结一下:
$user_id = Yii::$app->user->getID();
$profile = Profile::find()->where(['user_id' => $user_id)->one();