Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii 2 : override user model

Tags:

model

yii2

How to override user model properly in yii2?

For example, I want to override common\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 get error Setting unknown property: frontend\models\User::identityClass

like image 933
Natalie Avatar asked Dec 01 '14 14:12

Natalie


2 Answers

There are 2 things you can set up: 1)

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

and 2

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

You are switching them around. 'identityClass' is your model, 'class' is the the User component for Yii. By setting identityClass you are telling Yii that the User Component should use frontend\models\User it's identityClass property.

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

The previous versions of Yii2 had actually the file frontend\components\User.php already put in (that was extending yii\web\User). I can see why it is more confusing now.

What is the new error you are getting?

like image 79
Mihai P. Avatar answered Dec 18 '22 03:12

Mihai P.


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

in config + delete browser cookies ( seems to be yii2 bug- with cookies present, function renewAuthStatus() loaded common/models/User as identityClass and produced another error)

like image 43
Natalie Avatar answered Dec 18 '22 02:12

Natalie