model->save() Not Working In Yii2

后端 未结 10 1518
情歌与酒
情歌与酒 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:50

    Check model saving error like this :

    if ($model->save()) {
    
    } else {
      echo "MODEL NOT SAVED";
      print_r($model->getAttributes());
      print_r($model->getErrors());
      exit;
    }
    
    0 讨论(0)
  • 2020-12-06 01:53

    And there maybe another reason of not saving model - you have property of your Users class and before saving from form its reset to NULL.

    So, if you set $model->saveAttributes('favorite_book'=>$model->favorite_book), but at that time you declared in class Users public $favorite_book - you will get this field empty in DB.

    0 讨论(0)
  • 2020-12-06 01:54

    As @scaisEdge suggest, try removing all table related field in your Users class

    class Users extends ActiveRecord implements IdentityInterface 
    {
      /* removed because this properties is related in a table's field 
      public $first_name;
      public $last_name;
      public $email;
      public $password;
      public $user_type;
      public $company_name;
      public $status;
      public $auth_key;
      public $confirmed_at;
      public $registration_ip;
      public $verify_code;
      public $created_at;
      public $updated_at;
      public $user_type;
      public $company_name;
      public $status;
      public $auth_key;
      public $confirmed_at;
      public $registration_ip;
      public $verify_code;
      public $created_at;
      public $updated_at;
      */
    
      // this is properties that not related to users table
      public $rememberMe;
      public $confirm_password;
      public $_user = false;
    
      public static function tableName() {
        return 'users';
      }
    
     /* ........... */
    
    }
    
    0 讨论(0)
  • 2020-12-06 01:54

    The other solution mentioned $model->save(false);. That is just a temporary workaround, and you should still find the actual reason why the save functionality is not working.

    Here are additional steps to help diagnose the actual issue:

    1. check that _form input field has the proper name, and
    2. check that if you have added any dropdown functionality, then check whether it's working properly or not
    0 讨论(0)
  • 2020-12-06 01:55

    I guess $model->load() returns false, call $model->errors to see model's error.

    $model->load();
    $model->validate();
    var_dump($model->errors);
    
    0 讨论(0)
  • 2020-12-06 01:55

    You are doing all staff correct. I think u must add one line for confirm password validation

    if(!$CheckExistingUser) {    
    $auth_key = $model->getConfirmationLink();
            $password = md5($post['password']);
            $registration_ip = Yii::$app->getRequest()->getUserIP();
            $created_at = date('Y-m-d h:i:s');
    
            $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;
    

    And Also after this condition check model attributes and error like this :

    if($model->save()) {
              print_r("asd");
            }else{
    var_dump($model);exit;}
    
    0 讨论(0)
提交回复
热议问题