Best Practice To Insert 2 Diferents Models

Hello everyone,

I Have 2 models:







When i insert a client instance at the DB, i need to fill the person model before insert the client. I’m trying to put everything inside of my Client model to test the model and not the controller (that’s the way that i understand unit tests works. Correct me if i’m wrong please)…

I tried listening a EVENT_BEFORE_INSERT, and fill the person’s model, save and fill the id_person field in a event function, but it doens’t works because the validation come first and id_person is a required field.

I can’t do it in validation event because of the transaction, can i?

Anyway, what is the best way to do this?

Thank you

Have you checked beforeSave?

Stefano, thank you.

I used the event that trigger beforeSave to set the another model, but as i sad, i cant do this because my validation rules has a rule that block before the save method.

your can set id_person as a safe attribute becouse you set the value by manual