# Insert multiple select field with associated model

**URL:** <https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020>\
**Category:** Uncategorized\
**Created:** [February 16, 2021, 2:44pm UTC](https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020 "2021-02-16T14:44:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ftbs](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@Ftbs](https://discourse.cakephp.org/u/Ftbs)\
**Post date:** [February 16, 2021, 2:44pm UTC](https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020/1 "2021-02-16T14:44:18Z")

</div>

Hello guys. I’m having trouble inserting values ​​into two different tables. If I use multiple select, there is no insertion in the database. If I leave the select without being multiple there is insertion in the database. Any suggestion?

BeforeMarshal with multiple select false:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/919e7b833370270054d3200df65848c0c0ab3b90.png)

BeforeMarshal with multiple select true:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/5/5e18cf27f56091321d8936312b2f095550750211.png)

---

<div class="post-metadata">

**Author:** ![calzone](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/calzone/32/305_2.png) [@calzone](https://discourse.cakephp.org/u/calzone)\
**Post date:** [February 16, 2021, 3:11pm UTC](https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020/2 "2021-02-16T15:11:17Z")

</div>

Total speculation here, but my first instinct would be to check the models. Have you ruled that out yet? (Multiple select requires belongsToMany be properly set up and that validation and rules are in agreement)

---

<div class="post-metadata">

**Author:** ![Ftbs](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@Ftbs](https://discourse.cakephp.org/u/Ftbs)\
**Post date:** [February 16, 2021, 3:15pm UTC](https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020/3 "2021-02-16T15:15:19Z")

</div>

class SisadDemandSectorInvolvedsTable extends Table  
{  
public function initialize(array $config)  
{  
$this-\>belongsToMany(‘SisadDemands’, [  
‘foreignKey’ =\> ‘sisad\_demand\_id’,  
]);  
}  
}

and…

class SisadDemandsTable extends Table  
{  
public function initialize(array $config)  
{  
$this-\>hasMany(‘SisadDemandSectorInvolveds’, [  
‘foreignKey’ =\> ‘sisad\_demand\_id’,  
‘joinType’ =\> ‘INNER’  
]);  
}  
}

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 16, 2021, 5:11pm UTC](https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020/4 "2021-02-16T17:11:59Z")

</div>

For multiple selections, you need to use the [`_ids` field](https://book.cakephp.org/3/en/orm/saving-data.html#converting-belongstomany-data).

---

<div class="post-metadata">

**Author:** ![Ftbs](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@Ftbs](https://discourse.cakephp.org/u/Ftbs)\
**Post date:** [February 16, 2021, 9:51pm UTC](https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020/5 "2021-02-16T21:51:27Z")

</div>

you already got it? I have no idea how to do it

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 16, 2021, 11:41pm UTC](https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020/6 "2021-02-16T23:41:43Z")

</div>

As a first attempt, try replacing `sisad_demand_sector_involveds.sector_id` in your form with `sisad_demand_sector_involveds._ids`, and see how the entity looks after patching.

---

<div class="post-metadata">

**Author:** ![Ftbs](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@Ftbs](https://discourse.cakephp.org/u/Ftbs)\
**Post date:** [February 17, 2021, 11:53am UTC](https://discourse.cakephp.org/t/insert-multiple-select-field-with-associated-model/9020/7 "2021-02-17T11:53:23Z")

</div>

I tried and it still didn’t work.
