简体   繁体   中英

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. For some reason, when passing through the 'login' action in my controller ::

    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.

<?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 ..

if this you must remove the (redifined) public vars in your model

otherwise if you have no rules then

Try adding safe at the attribute in you model rules

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. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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