I’m just getting started on Yii2 (have done a couple of projects with Yii 1.1 earlier, both for personal use and professional), and it sure looks promising!
But I’ve run into a problem. I want to add some of behaviours to bascially all of my models, but from what I can understand that means that I have to copy & paste the same behaviour code in every model (50+).
I need autotimestamp for created_at / updated_at fields (when saving).
Also I would like to format my DATETIME fields to TIMESTAMP, when reading data from the database (and reversing from TIMESTAMP to DATETIME when saving).
And the third thing I’d like to do, is to add a created_by / updated_by field (user_id) when saving.
Isn’t there some way that I can attach these behaviours on all models without copying the same code into every model? Or is there another way of achieving this that I haven’t thought of?
The only conclusion I’ve come up with is to create models/BaseModel.php, have it extend the ActiveRecord and the make sure that all the desired models extends this BaseModel, but it feels like I’m “doing it all wrong” somehow…
Ok… so know I have the base model working as inteded!
95% of all tables have created_at/by
80% of all tables have created_at/by & updated_at/by
5% of all tables have neither
I’m trying to get the base model to be able to tell if created / updated fields exist for a given table, since it will throw an error complaining othervise… I’ve tried different solutions, but they all end up being very “hackish” - and I would prefer to avoid having different base classes for different models, if the table has “created”, “created” & “updated” or none of those fields.
Does Yii2 come with any suggestions on how to handle these scenarios?