简体   繁体   English

Yii2 save()使用默认值创建DB行

[英]Yii2 save() creating DB row with default values

I am trying to implement a login method using OpenID, and the $_SESSION var's are posting correctly - and through those I am simply trying to register users in MySQL. 我正在尝试使用OpenID实现一个登录方法,并且$ _SESSION var正在正确发布 - 通过这些我只是尝试在MySQL中注册用户。 For some reason, when passing through the 'login' action in my controller :: 出于某种原因,当我在我的控制器中执行'login'操作时::

    public function actionLogin()
{
    if (!Yii::$app->user->isGuest) {
        return $this->goHome();
    }

    include ('../views/user-record/steamauth/userInfo.php');

    $steamid = $_SESSION['steam_steamid'];
    $username = $_SESSION['steam_personaname'];
    $profileurl = $_SESSION['steam_profileurl'];
    $avatar = $_SESSION['steam_avatar'];
    $avatarmedium = $_SESSION['steam_avatarmedium'];
    $avatarfull = $_SESSION['steam_avatarfull'];

    $user = UserRecord::findBySteamId($steamid);

    if ($user === null)
    {
        $user = new UserRecord();

        $user->steamid = $steamid;
        $user->username = $username;
        $user->profileurl = $profileurl;
        $user->avatar = $avatar;
        $user->avatarmedium = $avatarmedium;
        $user->avatarfull = $avatarfull;
        $user->verified = 0;
        $user->banned = 0;

        $user->save();
    }

    Yii::$app->user->login($user, 604800);

    return $this->redirect(Yii::$app->user->returnUrl);
}

EDIT: Here is the UserRecord class, forgot to add it in. 编辑:这是UserRecord类,忘了添加它。

<?php

namespace app\models;

class UserRecord extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
    public $id;
    public $steamid;
    public $username;
    public $profileurl;
    public $avatar;
    public $avatarmedium;
    public $avatarfull;

    public $verified;
    public $banned;
    public $rank;
    public $authKey;

//    public $password;
//    public $accessToken;

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

    public function getAuthKey() 
    {
        return $this->authKey;
    }

    public function getId()
    {
        return $this->id;
    }

    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    public static function findIdentity($id)
    {
        return self::findOne($id);
    }

    public function validateSteamID($steamid)
    {
        return $this->steamid === $steamid;
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new \yii\base\NotSupportedException;
    }

    public static function findBySteamId($steamid)
    {
        return self::findOne(['steamid' => $steamid]);
    }

}

The result is simply a posted row, with none of the data entered. 结果只是一个已发布的行,没有输入任何数据。

Any help would be greatly appreciated, thank you. 非常感谢任何帮助,谢谢。

If you have redefine the same columns name using public vars these override the vars for activeRecord and then are saved only empty value .. 如果您使用公共变量重新定义相同的列名称,则覆盖activeRecord的变量,然后仅保存为空值。

if this you must remove the (redifined) public vars in your model 如果这样你必须删除模型中的(redifined)公共变量

otherwise if you have no rules then 否则如果你没有规则那么

Try adding safe at the attribute in you model rules 尝试在模型规则中的属性上添加safe

public function rules()
{
   return [
      [['steamid', 'username', 'profileurl', 'avatar', 'avatarmedium', 
               'avatarfull', 'verified', 'banned', 'rank', 'authKey',], 'safe'],
   ];
 }

Declaring 'public' variables made the save() ignore the data being posted. 声明'public'变量使save()忽略了发布的数据。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM