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
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?
'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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With