# Problem in saving belongsToMany Table

**URL:** <https://discourse.cakephp.org/t/problem-in-saving-belongstomany-table/1048>\
**Category:** Need Help\
**Created:** [August 25, 2016, 2:20pm UTC](https://discourse.cakephp.org/t/problem-in-saving-belongstomany-table/1048 "2016-08-25T14:20:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nir1978](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@nir1978](https://discourse.cakephp.org/u/nir1978)\
**Post date:** [August 25, 2016, 2:20pm UTC](https://discourse.cakephp.org/t/problem-in-saving-belongstomany-table/1048/1 "2016-08-25T14:20:48Z")

</div>

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.
