dongyun234854 2014-12-20 01:49 采纳率: 0%
浏览 43
已采纳

如何在LoginForm中更改表用户

I am asking about LoginForm in Yii2

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

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.

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

Thanks.

  • 写回答

1条回答 默认 最新

  • dongyan7876 2014-12-20 10:22
    关注

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

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

    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}}';
    }
    

    Change that to:

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

    Good luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集