Yii2 active record model not saving data

后端 未结 7 659
情话喂你
情话喂你 2020-12-16 17:47

I\'ve built a simple form model & view, a simple AR model, and a simple controller. The form model assigns the correct values to the AR instance, but when I call save(),

7条回答
  •  抹茶落季
    2020-12-16 18:19

    I ran across the same problem recently, when I combine the Active Record class with The Model class. Cause I know that AR actually extends Model in Yii2. Why not write less code.So I move the code from the Model to the AR.

    $model = new User();  
    $model->load(Yii::$app->request->post())
    

    But the AR's _attribute didn't get the post data in the form. the form data is actually in a Model object.

    object(app\models\User)#39 (12) { ["password"]=> string(6) "google" ["newpass"]=> NULL ["name"]=> string(5) "Jane1" ["email"]=> string(16) "jane@outlook.com" ["_attributes":"yii\db\BaseActiveRecord":private]=> array(2) { ["password_hash"]=> string(60) "$2y$13$.vNKpmosLjW/oYAhIezOZOj8rIG6QJvQj8tGHN2x78.75poXVn6Yi" ["auth_key"]=> string(32) "4XggNakVd-oeU28ny7obdw7gOmZJ-Rbu" }

    simply delete the public attribute you want mass assign to the AR instance will make it work.

提交回复
热议问题