I have 3 tables in the DB; Profiles, Submenus and Profiles_Submenus
//ProfilesTable
profiles belongsToMany(‘Submenus, [‘foreignKey’ => profile_id’, ‘targetforeignKey’ => ‘submenu_id’, ‘joinTable’ => ‘profiles_submenus’]);
//SubmenusTable
submenus belongsToMany(‘Profiles’, [‘foreignKey’ => ‘submenu_id’, ‘targetforiegnKey’ => ‘profile_id’, ‘joinTable’ => ‘profiles_submenus’]);
I want to add Profiles and select submenus for the profile. I created the form that lists existing submenus that need to be selected. I get the submenu_id(s) in the post, below is the debug.
object(App\Model\Entity\Profile) {
'name' => 'Profile 3',
'location_id' => (int) 1,
'Submenus' => [
'_ids' => [
(int) 0 => '9',
(int) 1 => '11'
]
],
'id' => (int) 16,
'[new]' => false,
'[accessible]' => [
'*' => true,
'submenus' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Profiles'
}
but when I look into the tables, I only have a new profile record, but in profiles_submenus, there is no records.
profiles_submenus Table
profile_id | submenu_id
the add function in ProfilesController:
$profile = $this->Profiles->newEntity();
if ($this->request->is(‘post’)) {
$profile = $this->Profiles->patchEntity($profile, $this->request->data, [‘associated’ => [‘Submenus’]]);
$profile->dirty(‘submenu’,true);
if ($this->Profiles->save($profile, [‘associated’ => [‘Submenus’]])) {
debug($profile);
$this->Flash->success(__(‘Profile “{0}” has been saved.’, $this->request->data[‘name’]));
// return $this->redirect(['action' => 'index']);
}
Please help what is wrong.