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

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法