doutian3269 2017-02-16 03:50
浏览 68
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dougao7801 2017-02-16 12:28
    关注

    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'],
       ];
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?