Model::load() Returning False

So I have a form model, on submission after I do some processing I use the form model’s getAttributes() method to return certain attributes to set on the AR model. However, the AR’s load method is returning false


if($form_model->load(Yii::$app->request->post()) && $form_model->validate()) {

   $attributes = $form_model->getAttributes(null, ['display_image', 'tag']);

   if($db_model->load($attributes) && $db_model->save()) {

       // do other DB stuff and redirect

   }

}

Does load just not like a normal array? I tried it this way because directly passing the array to the AR when it’s instantiated said that one of the properties (a foreign key) was read-only.

So two things:

  1. Why is load possibly failing?

  2. Why does the AR throw a read-only error for the foreign key property when the array is passed when it’s instantiated? (the original way I tried it) - figured this one out it’s because of a ‘get’ method.

Edit:

I also have a field that’s optional on edit, but required when the submission is new. However, if the field doesn’t have a value the JavaScript won’t allow the form to submit. Is there a way around that? I didn’t see anything in ActiveForm about it.

Depends… http://www.yiiframework.com/doc-2.0/yii-base-model.html#load()-detail

Well I’m passing an plain array to it with no formName, yet it still doesn’t set the data. So I dunno. :mellow:




   if($db_model->load($attributes, "") && $db_model->save()) {