Best way to increment column value before insert to db

Hello.

What is the best way to increment column value in row before insert in Yii2? (which is not primary key - i don’t want also to use sql autoincrement). I need to get max value of one column from all rows in db before insert a new row and increment it by 1 and then assign this value to active record model field. I want this solution to be universal - where i would set only column name (i don’t want to repeat code in many models). Should it be behavior or extension or component?

Thank you for help

Greetings

Tom

Override beforeSave method (https://www.yiiframework.com/doc/api/2.0/yii-db-baseactiverecord#beforeSave()-detail) and set the specific parameter.