我想根据我的具体要求创建自定义userIdentity类。这里的代码是
<?php
namespace app\models;
use yii\web\IdentityInterface;
use app\models\dbTables\Users;
class UserIdentity implements IdentityInterface{
const ERROR_USERNAME_INVALID=3;
const ERROR_PASSWORD_INVALID=4;
const ERROR_NONE=0;
public $errorCode;
private $_id;
private $_email;
private $_role;
private $_name;
public function findIdentityById($id){
$objUserMdl = new Users;
$user = $objUserMdl::findOne($id);
$userRole = $objUserMdl->getUserRole($user->user_id);
$this->_id = $user->user_id;
$this->_email = $user->email_address;
$this->_role = $userRole;
$this->_name = $user->full_name;
return $this;
}
public function getId()
{
return $this->_id;
}
public function getName(){
return $this->_name;
}
public function getEmail(){
return $this->_email;
}
public function getRole(){
return $this->_role;
}
public static function findIdentity($id)
{
return self::findIdentityById($id);
}
public function getAuthKey()
{
throw new NotSupportedException('"getAuthKey" is not implemented.');
}
public function validateAuthKey($authKey)
{
throw new NotSupportedException('"validateAuthKey" is not implemented.');
}
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
}
?>
基本上我有两个表角色和用户,我想在yii :: $ app-&gt; user-&gt; identity
中设置两个表中的特定属性当我调用上面的代码时,findIdentity($id)
函数返回错误,原因很明显,我说我无法在静态功能中调用$this
。如何在函数中设置所需的属性,从中返回userIdentity类的实例?
答案 0 :(得分:0)
我建议阅读:When to use self over $this?你真的很困惑2.
$objUserMdl = new Users;
$user = $objUserMdl::findOne($id);
$userRole = $objUserMdl->getUserRole($user->user_id);
你在一个对象上调用::你不能这样做。
我说要删除你所做的并重新开始,它应该比你写的要容易得多。需要很长时间才能告诉你如何正确地做到这一点,只需查看yii2高级模板,看看他们是如何做到的。您可以使用自己的标识类并在那里设置任何特殊属性。只需研究yii2代码。