# Belongs to Many: save custom field

**URL:** <https://discourse.cakephp.org/t/belongs-to-many-save-custom-field/6405>\
**Category:** Need Help\
**Created:** [July 23, 2019, 9:26am UTC](https://discourse.cakephp.org/t/belongs-to-many-save-custom-field/6405 "2019-07-23T09:26:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlzGamer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/alzgamer/32/2144_2.png) [@AlzGamer](https://discourse.cakephp.org/u/AlzGamer)\
**Post date:** [July 23, 2019, 9:26am UTC](https://discourse.cakephp.org/t/belongs-to-many-save-custom-field/6405/1 "2019-07-23T09:26:22Z")

</div>

Hi, I need help with saving/updating `belongsToMany` data with custom field.

I have a tables `acl`, `managers`, `groups`, `sections`.

I need to save/update data in `acl` table from `sections/edit` or `sections/add`

`acl` table

| id | target\_id | target\_type | section\_id |
| --- | --- | --- | --- |
| \* | \* | \* | \* |

I need set `target_id` data ‘user’ or ‘group’ depending entities.

_ **`edit.ctp`** _

```php
echo $this->Form->control('managers._ids', ['class' => 'multiple', 'multiple' => true]);
echo $this->Form->control('groups._ids', ['class' => 'multiple', 'multiple' => true]);

```

_ **`SectionsTable.php`** _

```php
$this->belongsToMany('Managers', [
    'joinTable' => 'acl',
    'through' => 'Acl',
    'foreignKey' => 'section_id',
    'targetForeignKey' => 'target_id',
    'conditions' => [
        'target_type' => 'user'
    ]
]);
$this->belongsToMany('Groups', [
    'joinTable' => 'acl',
    'through' => 'Acl',
    'foreignKey' => 'section_id',
    'targetForeignKey' => 'target_id',
    'conditions' => [
        'target_type' => 'group'
    ]
]);

```

_ **`AclTable.php`** _

```auto
$this->belongsTo('Managers', [
    'foreignKey' => 'target_id',
    'conditions' => [
        'target_type' => 'user'
    ]
]);
$this->belongsTo('Groups', [
    'foreignKey' => 'target_id',
    'conditions' => [
        'target_type' => 'group'
    ]
]);

```

I tried to use `_joinData`, but nothing work

before and after `patchEntity()`

```auto
foreach ($section->groups as &$group) {
    $group->_joinData = ['target_type' => 'group'];
}

```

whithout `save()`

```auto
foreach ($this->request->data['managers']['_ids'] as $id) {
    $manager = $this->Sections->Managers->get($id);
    $manager->_joinData = new Entity(['target_type' => 'user'], ['markNew' => true]);
    $this->Sections->Managers->link($section, [$manager]);
}

```

`$this->request->data` has a follow structure:

```php
[
    'controller' => '*',
    'action' => '*',
    'title' => '*',
    'managers' => [
        '_ids' => [
            '0' => '*',
            '1' => '*',
            '2' => '*',
        ]
    ],
    'groups' => [
        '_ids' => [
            '0' => '*',
            '1' => '*',
            '2' => '*',
        ]
    ]
]

```

Alltimes error: `Cannot insert row, some of the primary key values are missing. Got (3, 114, ), expecting (target_id, section_id, target_type)`

---

<div class="post-metadata">

**Author:** ![AlzGamer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/alzgamer/32/2144_2.png) [@AlzGamer](https://discourse.cakephp.org/u/AlzGamer)\
**Post date:** [July 23, 2019, 11:46am UTC](https://discourse.cakephp.org/t/belongs-to-many-save-custom-field/6405/2 "2019-07-23T11:46:57Z")

</div>

Now working with following code

```php
$section = $this->Sections->patchEntity($section, $this->request->data, ['associated' => ['Managers', 'Groups']]);
foreach ($section->managers as &$manager) {
    $manager->_joinData = new Entity([
        'target_id' => $manager->id,
        'target_type' => 'user',
        'section_id' => $section->id
    ], ['markNew' => true]);
}
foreach ($section->groups as &$group) {
    $group->_joinData = new Entity([
        'target_id' => $group->id,
        'target_type' => 'group',
        'section_id' => $section->id
    ], ['markNew' => true]);
}

if ($this->Sections->save($section, ['associated' => ['Managers', 'Groups']])) {
    $this->Flash->success(__('The section has been saved.'));

    return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The section could not be saved. Please, try again.'));

```

But, maybe can do it don’t listing all fields?  
Also I need to create/add new records to `acl` table when adding new `section`  
But `section_id` unavailable until `save()`. Can I save/add `acl` records by associations

---

<div class="post-metadata">

**Author:** ![AlzGamer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/alzgamer/32/2144_2.png) [@AlzGamer](https://discourse.cakephp.org/u/AlzGamer)\
**Post date:** [July 25, 2019, 6:31am UTC](https://discourse.cakephp.org/t/belongs-to-many-save-custom-field/6405/4 "2019-07-25T06:31:23Z")

</div>

## **Updated**

_ **`AclTAble.php`** _

```auto
$this->setPrimaryKey(['target_type', 'target_id', 'section_id']);
.
.
.
$this->belongsTo('Managers', [
    'foreignKey' => 'target_id',
    'conditions' => [
        'target_type' => 'user'
    ]
]);
$this->belongsTo('Groups', [
    'foreignKey' => 'target_id',
    'conditions' => [
        'target_type' => 'group'
    ]
]);

```

With primaryKey `id`, adding and editing entities working, but editing makes dublicates, it’s are not perfect for me. And in documentations use composite primaryKey, so i use `['target_id', 'target_type', 'section_id']`, with it cake don’t make dublicated, but aggain show me error: `Cannot insert row, some of the primary key values are missing. Got (8, 197, ), expecting (target_id, section_id, target_type)`
