Yii2无法保存

时间:2015-07-19 16:12:48

标签: php yii2

我正在尝试在Yii2(高级模板)中实现用户配置文件。我已经将Gii CRUD生成器用于User模型,并将“view”重命名为“profile”(在控制器,视图等中)。但是当我尝试保存更新的数据时,$model->save()正在返回true时,它不会保存在数据库中。

型号:

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;
}

控制器操作:

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,
        ]);
    }
}

标准ActiveForm在View中有1个字段(由于某种原因无法正确粘贴到此处,抱歉)。我是否需要添加其他文件代码?可能是什么问题?

UPD:当我创建新用户时,它是使用空字段创建的,无论我是否在那里写字。

2 个答案:

答案 0 :(得分:0)

好的,我发现了问题所在。 模型编辑:

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],**
    ];
}

添加了此规则并设法更改了值:)

答案 1 :(得分:0)

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

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