I'm starting with CakePHP. I have very simple models:
- Users
- Companies
- CompaniesUsers (a hasMany through relation between Users and Companies)
I create a user, and when I login as this user, if there isn't any relation between User and Companies, I'm redirected to the add action of the Companies controller. This is all done, and working well.
My "big" problem is: When I save a company, I must save the company's data AND the relation between the current user and the created company. This all must be done inside a transaction, because I don't want "orphan" companies. But where should I put this code? Create a save() function in the Companies model and create the relation after saving? Create an afterSave() function in the Companies model? Put all the code in the CompaniesController's add action? I don't think this last option is a good idea, the bussiness logic must be in the model, isn't it?
I know that this must be a very basic question, but I'm new to CakePHP and to MVC.