# Save items from multiple select - array values

**URL:** <https://discourse.cakephp.org/t/save-items-from-multiple-select-array-values/10289>\
**Category:** Need Help\
**Created:** [April 8, 2022, 5:20pm UTC](https://discourse.cakephp.org/t/save-items-from-multiple-select-array-values/10289 "2022-04-08T17:20:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [April 8, 2022, 5:20pm UTC](https://discourse.cakephp.org/t/save-items-from-multiple-select-array-values/10289/1 "2022-04-08T17:20:54Z")

</div>

I have table `products_items_colors` but colors in array PHP - both column is primary key.

In table is fields:

```auto
products_item_id - int
color -int

```

```auto
$color = [
    1 => 'Black',
    2 => 'Green',
    3 => 'Blue',
];

```

Form is:

```auto
echo $this->Form->control('products_items_colors._ids', [
    'type' => 'select',
    'multiple' => true,
    'options' => [
        1 => 'Black',
        2 => 'Green',
        3 => 'Blue',
    ],
]);

```

In model `ProductsItems` table

```auto
$this->hasMany('ProductsItemsColors', [
    'foreignKey' => 'products_item_id',
]);

```

In model `ProductsItemsColors` table

```auto
$this->setTable('products_items_colors');
$this->setDisplayField(['color', 'products_item_id']);
$this->setPrimaryKey(['color', 'products_item_id']);

$this->belongsTo('ProductsItems', [
    'foreignKey' => 'products_item_id',
    'joinType' => 'INNER',
]);

```

After save form - patch entity does not contain items. But if I put data into the database, the form shows me the values.

How do I save this data to a table?

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [April 8, 2022, 6:20pm UTC](https://discourse.cakephp.org/t/save-items-from-multiple-select-array-values/10289/2 "2022-04-08T18:20:32Z")

</div>

make sure `products_items_colors` is set as asscessible in your entity class `src/Model/Entity/MyEntity.php`

```auto
    protected $_accessible = [
        // other fields
        'products_items_colors' => true,
    ];

```

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [April 8, 2022, 6:22pm UTC](https://discourse.cakephp.org/t/save-items-from-multiple-select-array-values/10289/3 "2022-04-08T18:22:59Z")

</div>

Yes it is - `src/Model/Entity/ProductItem.php`

After patch entity result is:

```auto
'products_items_colors' => []

```

Probably this should be in the `ProductsItems` table, I just don’t know how to set the parameters exactly … if I use `_ids` in the form

```auto
$this->belongsToMany('ProductsItemsColors', [
    'foreignKey' => 'products_item_id',
    ...
]);

```
