V
V
Vladislav2020-07-02 10:25:10
Yii
Vladislav, 2020-07-02 10:25:10

Why is the save() method not working?

I have a method like this:

public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {

            $this->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);

            return true;
        }
        return false;
    }
public function afterSave($insert, $changedAttributes)
    {
        # Добавляем пользователю группу пользователя
        $auth = Yii::$app->authManager;
        $user_group = $auth->getRole('user');
        $auth->assign($user_group, $this->id);
        parent::afterSave($insert, $changedAttributes);
    }

Before saving, we generate a password for the user.

Now there was such a problem that when trying to update user data, for example, the name.
$model = Users::findOne(['id'=>1]);
$model->name = 'New name';
$model->save();


$model->save() - returns false.
$model->validate() - returns false;
$model->getErrors() - returns an empty array.

Here is the process of changing the field, changing the balance:
//$user = Users::findOne(['id'=>Yii::$app->user->identity->id]);
$user = Users::find()->where(['id' => Yii::$app->user->identity->id])->one();
        $user->balance = ($user->balance - $cost);
        
        if (!$order->save() || !$user->update()) {
            $response = ['code' => -99, 'text' => 'Произошла неизвестная ошибка, обратитесь в тех поддержку, код ошибки: -99'];
            $this->printAPI($response);
        } else {
            $response = ["code" => 5, "text" => "Заказ №" . $order->id . " успешно создан! ", 'balance' => $user->balance];
            $this->printAPI($response);
        }

What could be the problem? Why doesn't it save, I sin on the after and berfore save methods.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2020-07-02
@cr1gger

You are not validating. See validation rules. Execute in code var_dump($model->errors) перед формой.AND see errors. If you do not have errors of one model - see another. Whether it's validation or not is easy to check.$model->save(false)

I
Ivan Orlov, 2020-07-02
@Demisang

1. You can not write

$this->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);

In the after/beforeSave() method, because the first time we assume $password will be normal, and then pulling the user out of the database, his password will already be a hash, and saving the user again, a new password hash will be generated from the old password hash, and so on, in short after the second save, the user's password will be completely unusable.
2. Cannot be used $user->update()in the construction if, because $user->update() returns not true / false, but the number of changed rows in the database, but in Yii, if the model has not changed, nothing will be updated in the database (why an extra request), just write$user->save()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question