How to modify data before saving it into database in yii?

we sometimes need to modify user submitted data or based on user submitted data we need to do some calculation to fetch the values then, we will save that data in the database, so in order to achieve this, we will use modal events in YII.

For example, you might want to do API requests to fetch some values before a model instance is saved. To do so, just create a beforeSave() method within the model:

you can modify the data the way you want before saving it using beforeSave event.Based on isNewRecord property check, we can have different logic for the update and new records insert.

That’s it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets. Protection Status