yii 2:覆盖用户模型 [英] yii 2 : override user model

查看:70
本文介绍了yii 2:覆盖用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在yii2中正确覆盖用户模型?

How to override user model properly in yii2?

例如,我要覆盖common \ models \ User.php

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

我使用以下代码创建了frontend \ 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}}';
  }
  ...

在main.php中,当我添加

In main.php, when I add

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

我得到错误 设置未知属性:frontend \ models \ User :: identityClass

I get error Setting unknown property: frontend\models\User::identityClass

推荐答案

您可以设置以下两项: 1)

There are 2 things you can set up: 1)

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

和2

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

您正在切换它们. 'identityClass'是您的模型,'class'是Yii的User组件.通过设置identityClass,您告诉Yii用户组件应使用frontend \ models \ User的identityClass属性.

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.

这是用户组件 https://github.com /yiisoft/yii2-framework/blob/master/web/User.php

以前版本的Yii2实际上已经放了文件frontend \ components \ User.php(正在扩展yii \ web \ User).我明白了为什么现在更加混乱了.

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?

这篇关于yii 2:覆盖用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆