Modify Data Before Insert/update

I’m having a model in which I have to save datetime values with ms.

Unfortunately my mysql version does not support milliseconds yet.

That’s why I’m storing the values as decimals (format “U.u”).

In my form I have added a custom widget (extends InputWidget)

that transforms the decimal value to my display format "Y-m-d H:i:s.u".

In my controller I call a new function during the actionCreate and actionUpdate function.

This function transforms the input back from my display format to the decimal format.

This works, but I’m wondering if there is nicer way to do this?

Can I somehow use scenario’s to transform these fields during actionCreate/actionUpdate?

Add it to your model class via ActiveRecord::beforeSave()

Tried that using:




public function beforeSave($insert) {

  if (parent::beforeSave($insert)) {

    //  do transformation here

  }

}



But because the format posted by the model is a string the decimal check fails.

Is it safe to do the transformation before the parent::beforeSave call?

Hmm, not sure what you mean by decimal check - Model::beforeSave() occurs after validation, so it shouldn’t be checking anything.

Are you sure you’re returning a boolean? Typically I use beforeSave() like this:




public function beforeSave($insert) {

  

  // do transformations here

  

  return parent::beforeSave($insert);

}