On a project I’m working on I have memberships that can become activated, and after they are activated I send out an email. I noticed that just saving the model after submitting the form to save it was always sending out an email even if I wasn’t changing the active value.
I looked into why this might be happening by checking the $changedAttributes value in the afterSave() function on an ActiveRecord model. I noticed that the active field was in changed attributes with a value of 1 as an int using var_dump on the $changedAttributes array. I did a var_dump on the model itself after save and the active field had a value of 1 as a string. I thinkloading in the attributes using the ‘$model->load(Yii::$app->request->post())’ method is setting the active field as a string when it is originally loaded in as an ‘int’ out of the database, and this is what is causing it to appear as a changed attribute.
Should this be considered a bug? If not, is there a way that I am unaware of to make sure that a variable is correclty casted as it’s type when the attributes are loaded into the model?
I’ve got validation on the model requiring that the field is an integer and it’s passing. I could in my controller make sure that it’s correctly loaded as an (int) by reassigning it using $model->active = (int)$model->active. But I noticed I have several numerical variables this is happening to, and resetting these all individually would just clutter up my code I feel.
What do you guys think?