I’m sure there is a very simple solution to this, but the explanation is complex, so please do bear with me.
I have a registration form Registration::add() and it comprises of three sections.
- The first is the
Registrationdata, a number of fields. - The second is checkboxes from three related
HABTMmodels,Role,CategoryandCategoryChild - Third is creating a record in
User
My Registration model has a user_id and I need to save the generated User.id into that field.
I’m sure that I should be able to do this using the model relationships, as the User model saves the User.registration_id fine, but it doesn’t seem to be writing into the Registration model.
Do I need to create another field in my add.ctp view so that the field is present in the $this->data->request array? I would much rather do this, than get embroiled in faffing with beforeSave() and afterSave().
So do I just need to create
$this->Form->input('Registration.user_id', array('type'=>'hidden'));
My relationships, for reference,
Registration hasOne User
Registration hasAndBelongsToMany Category
Registration hasAndBelongsToMany CategoryChild
Registration hasAndBelongsToMany Role
User hasOne Registration
I’m currently using saveAll($this->request->data) to save all my data, and it’s managing to save everything except Registration.user_id
Your Relationships have
However you have both a
Registration.user_idand aUser.registration_idimplying that you need hasAndBelongsToMany relationship.Just in case you’re not using the Cake Baker to generate the models it is very useful.