Yii::app()->userYiiで情報を拡張する方法について、いくつかの(ほぼ同等の)例を見ました。これがそのうちの1つです。
コード(現状のまま):
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$user=User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($user->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->setState('lastLoginTime', $user->lastLoginTime); // added property
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
lastLoginTimeその後、次のように値にアクセスできることが確認されています。
Yii::app()->user->lastLoginTime
ここでの問題は、CUserIdentityであるが、 CWebUsersetStateであるを要求したことであり、CWebUserにはデフォルトでCUserIdentityへの参照がありません。$thisYii::app()->user
どちらも実際に追加のプロパティの保存setStateとを介した取得をサポートしていますgetStateが、最初は内部_state配列を使用し、2番目はすべてをに書き込みます$_SESSION。
したがって、問題は、拡張情報を1つのエンティティに書き込んでから、別のエンティティから読み取るにはどうすればよいかということです。これを提供できるYiiコードには何もありません。例ではエラーのように見えます。