model->save() Not Working In Yii2

后端 未结 10 1519
情歌与酒
情歌与酒 2020-12-06 01:10

Previously, I was not using $model->save() function for inserting or updating any data. I was simply using createCommand() to execute query and

相关标签:
10条回答
  • 2020-12-06 01:58

    It could be a problem related with your validation rules.

    Try, as a test, to save the model without any validation in this way:

    $model->save(false);
    

    If the model is saved you have conflict with your validation rules. Try selectively removing your validation rule(s) to find the validation conflict.

    If you have redefined the value present in active record you don't assign the value to the var for db but for this new var and then are not save.

    Try removing the duplicated var.. (only the vars non mapped to db should be declared here.)

    0 讨论(0)
  • 2020-12-06 02:00

    if your column type in your table is "integer" and your data is "string" you may see tis error.You should check your data type and try again.

    I suppose that your column type is integer, you should write the following code:

    $model->created_at=time();//1499722038
    $model->save(); 
    

    but your column type is string, you should write the following code:

    $model->created_at=date('d/m/Y');//11/07/2017
    $model->save(); 
    
    0 讨论(0)
  • 2020-12-06 02:09

    Try this:

    $model->save(false);
    

    and if thats working, check your model rules() and your form rules() if its having the same rules. usually the cause is the required fields in your table.

    0 讨论(0)
  • 2020-12-06 02:09

    in your model i found First name , last name , email , password is required fields and in your controller you are updating or saving only

     $model->auth_key = $auth_key;
            $model->password = $password;
            $model->confirm_password= md5($post["confirm_password"]);  /// add  this line
            $model->registration_ip = $registration_ip;
            $model->created_at = $created_at;
    

    but first name and last name and email id are required so it will throw validation error , to check this error use

    $model->load();
    $model->validate();
    var_dump($model->errors);
    

    it will show you the error . correct that errors then model will get save. you can solve that error using Scenario or

    $model->saveAttributes('favorite_book'=>$model->favorite_book,'favorite_movie'=>$model->favorite_movie);
    

    I hope it will help you.

    0 讨论(0)
提交回复
热议问题