0 レビュー
1 回答
php-yii2のカスタムuserIdentityクラス
特定の要件に従ってカスタム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.');
}
}
?>
基本的に2つのテーブルの役割とユーザーがあり、yii :: $ app->user->identityの両方のテーブルから特定のプロパティを設定したい
上記のコードを呼び出すと、静的関数で $this
を呼び出せないという明らかな理由でfindIdentity($id)
関数がエラーを返します。 。関数に必要なプロパティを設定し、そこからuserIdentityクラスのインスタンスを返すにはどうすればよいですか?
わからない
0
レビュー
答え :
解決策:
これを読むことをお勧めします: $thisよりも自分自身を使用する場合あなたは本当に2を混乱させています。
$objUserMdl = new Users;
$user = $objUserMdl::findOne($id);
$userRole = $objUserMdl->getUserRole($user->user_id);
オブジェクトに対して::を呼び出していますが、それはできません。
私はあなたがしたことを削除してやり直すと言います、それはあなたが書いたものよりはるかに簡単なはずです。それを適切に行う方法を示すには長い時間がかかります。yii2アドバンステンプレートを調べて、それらがどのように行っているかを確認してください。独自のIDクラスを使用して、そこに特別な属性を設定できます。 yii2コードを勉強するだけです。
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。