User Identity Default Scope Problem

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 am getting the following error




Undefined property: yii\web\User::$isGuest



Looks like some sort of conflict between


\yii\web\User

and


\app\models\User

but not sure how to resolve it.

Any ideas?

Anyone have any ideas????

How have you defined the user component in your config file?

Its something like this for an advanced app:




    'components' => [

        'user' => [

            'identityClass' => 'common\models\User',

            'enableAutoLogin' => true,

        ],

    ]



Yes thats how its defined, but using


'identityClass => 'app\models\User'

I have used default scope find methods on other model classes and can utilise methods/properties of


Yii::$app->user

class e.g.


Yii::$app->user->identity->role

with no issues, however when I do it with the User class I am getting the error

Having exactly the same problem.




namespace app\components;

	

use Yii;

use yii\behaviors\TimestampBehavior;

use yii\db\Expression;

	

class ActiveRecord extends \yii\db\ActiveRecord {


}



Class setup as above. Using …




Yii::$app->user->isGuest



Results in following error message …

Undefined property: yii\web\User::$isGuest

Any ideas why?

No one can see what might be causing this?

Just to add to this …




Yii::$app->user



Is definitely using the class yii\web\User.

If I do …




get_class(Yii::$app->user);



It returns …

yii\web\User

Which is correct I presume. Just seems weird how it can be undefined property error.

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.