douhuiyan2772 2015-07-19 16:12
浏览 156

Yii2无法从表单中保存用户数据

I'm trying to implement user profiles in Yii2 (advanced template). I've used Gii CRUD generator for the User model and renamed everywhere "view" to "profile" (in controller, views etc.). But when I try to save updated data, it is not saved in database while $model->save() is returning true.

Model:

public static function tableName()
{
    return '{{%user}}';
}


public function behaviors()
{
    return [
        TimestampBehavior::className(),
    ];
}


public function rules()
{
    return [
        ['status', 'default', 'value' => self::STATUS_ACTIVE],
        ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
        [['image'], 'file'],
    ];
}

public function attributeLabels()
{
    return [
        'username' => 'Логин',
        'email' => 'Email',
        'image' => 'Изображение',
        'status' => 'Статус',
        'info' => 'Информация о себе'
    ];
}

public static function findIdentity($id)
{
    return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}


public static function findIdentityByAccessToken($token, $type = null)
{
    throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}

public static function findByUsername($username)
{
    return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
}

public static function findByPasswordResetToken($token)
{
    if (!static::isPasswordResetTokenValid($token)) {
        return null;
    }

    return static::findOne([
        'password_reset_token' => $token,
        'status' => self::STATUS_ACTIVE,
    ]);
}

public static function isPasswordResetTokenValid($token)
{
    if (empty($token)) {
        return false;
    }
    $expire = Yii::$app->params['user.passwordResetTokenExpire'];
    $parts = explode('_', $token);
    $timestamp = (int) end($parts);
    return $timestamp + $expire >= time();
}

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

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

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

public function validatePassword($password)
{
    return Yii::$app->security->validatePassword($password, $this->password_hash);
}

public function setPassword($password)
{
    $this->password_hash = Yii::$app->security->generatePasswordHash($password);
}

public function generateAuthKey()
{
    $this->auth_key = Yii::$app->security->generateRandomString();
}

public function generatePasswordResetToken()
{
    $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}

public function removePasswordResetToken()
{
    $this->password_reset_token = null;
}

Controller action:

public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['profile', 'id' => $model->id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

Standard ActiveForm with 1 field in View (can't paste it here properly for some reason, sorry). Do I need to add another files code? What can be the problem?

UPD: When I create new User, it is created with empty fields whether I write there something or not.

  • 写回答

2条回答 默认 最新

  • duanhu7390 2015-07-19 16:56
    关注

    Okay, I've found out what was the problem. Model edit:

    public function rules()
    {
        return [
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
            [['image'], 'file'],
            **[['username', 'email'], 'string', 'max' => 255],**
        ];
    }
    

    Added this rule and managed to change the value :)

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端