Belongs to Many: save custom field

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

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

SectionsTable.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

$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()

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

whithout save()

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:

[
    '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)

Now working with following code

$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

Updated

AclTAble.php

$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)