Yii 2: override user model

How to properly override user model in yii2?

For example, I want to override general \ models \ User.php

I created frontend \ models \ User.php with the following code:

  namespace frontend\models;


  use common\models\User as BaseUser;

  class User extends BaseUser
  {

   public static function tableName()
  {
    return '{{%accounts}}';
  }
  ...

      

In main.php, when I add

     'user'=>array(
        'class' => 'frontend\models\User',
    ),

      

I am getting error Setting unknown property: frontend \ models \ User :: identityClass

+3


source to share


2 answers


There are two things you can customize: 1)

'user' => [
    'identityClass' => 'common\models\User',
],

      

and 2

'user' => [
    'class' => 'frontend\components\User',
],

      



You switch them. "identityClass" is your model, "class" is the User component for Yii. By setting the identityClass, you are telling Yii that the User component should use the frontend \ models \ User it identityClass property.

This is the user component https://github.com/yiisoft/yii2-framework/blob/master/web/User.php

In previous versions of Yii2, the frontend \ components \ User.php file was actually added (which yii \ web \ User extended). I can understand why this is confusing now.

What is the new error you are getting?

+4


source


'user' => [
   'identityClass' => 'frontend\components\User',
   ],   

      



in config + delete browser cookies (it seems yii2 error - with cookies present, renewAuthStatus () function loaded by common / models / User as identityClass and caused another error)

+1


source







All Articles