I am using a default scope find method on my User (identity) class but when I try to access any
Yii::$app->user
functions/properties. For example:
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
....
public static function find()
{
if (Yii::$app->user->isGuest) {
...
}
}
}
I had the same problem, and had a look at the stack trace to see if there was anything interesting.
It seems to only happen when you ‘log in via cookie’.
My particular issue was triggered in the ‘after login’ hook.
On further investigation, I noted this in the loginByCookie code:
if ($this->enableAutoLogin) {
if ($this->getIsGuest()) {
$this->loginByCookie();
} elseif ($this->autoRenewCookie) {
$this->renewIdentityCookie();
}
}
Here, the class us specifically using the getter function, getIsGuest(). If the developers of Yii did it this way, I expect there may be a problem around using these ‘getters’ in the web/user class before full login has been initiated.
I’ll leave this for a while and let you know if i get any other problems.