如何在 LoginForm 中更改表用户 [英] How to change table users in LoginForm

查看:27
本文介绍了如何在 LoginForm 中更改表用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问的是 Yii2 中的 LoginForm

I am asking about LoginForm in Yii2

安装 Yii 后,我得到了默认的 Web,其中包含登录表单.此表单将连接到表名user"

After I install Yii, I get the default Web with login form inside it. This form will connect to table name "user"

然后我修改默认值以使用不同的登录表单创建新网站.我还为登录名db_user"创建了新表.我仍然使用 commons/model 中名为LoginForm"的默认模型进行登录.这是代码

Then I modify the default to create new website with different login form. And also I create new table for login name "db_user". I still use the default model named "LoginForm" in commons/model for login. Here is the code

<?php
namespace common\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;

private $_user = false;


/**
 * @inheritdoc
 */
public function rules()
{
    return [
        // username and password are both required
        [['username', 'password'], 'required'],
        // rememberMe must be a boolean value
        ['rememberMe', 'boolean'],
        // password is validated by validatePassword()
        ['password', 'validatePassword'],
    ];
}

/**
 * Validates the password.
 * This method serves as the inline validation for password.
 *
 * @param string $attribute the attribute currently being validated
 * @param array $params the additional name-value pairs given in the rule
 */
public function validatePassword($attribute, $params)
{
    if (!$this->hasErrors()) {
        $user = $this->getUser();
        if (!$user || !$user->validatePassword($this->password)) {
            $this->addError($attribute, 'Incorrect username or password.');
        }
    }
}

/**
 * Logs in a user using the provided username and password.
 *
 * @return boolean whether the user is logged in successfully
 */
public function login()
{
    if ($this->validate()) {
        return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
    } else {
        return false;
    }
}

/**
 * Finds user by [[username]]
 *
 * @return User|null
 */
public function getUser()
{
    if ($this->_user === false) {
        $this->_user = User::findByUsername($this->username);
    }

    return $this->_user;
}


 }

阅读代码后我感到困惑,因为在此模型中没有声明将用于登录的表名.我尝试登录后,它只适用于记录在用户"表中的用户.

After I read the code I confuse, because in this model does not declare the table name that will be used for login. After i tried login it only work for users those were recorded in "user" table.

如何将默认表从user"更改为db_user"?

How can I change the default table from "user" to "db_user"?

谢谢.

推荐答案

LoginForm 模型只是表单的模型,而不是用户模型.

The LoginForm model is just a model for the form, not for the User model.

User::findByUsername($this->username);

如你所见,LoginForm 中使用的数据库模型是 User,如果你进入你的文件 models/User.php 你会看到一行:

As you see, the db model used in LoginForm is User, if you go in your file models/User.php you will see a line:

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

将其更改为:

public static function tableName()
{
    return 'db_user';
}

祝你好运

这篇关于如何在 LoginForm 中更改表用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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