BelongsToMany 关联记录不会保存在联接中 table

BelongsToMany associated record won't save in join table

我有 2 tables "Descriptions" 和 "Phases",根据 Cakephp 命名约定,使用连接 Table "DescriptionsPhases" 关联 BelongsToMany。我一直在努力解决这一天的大部分时间:我无法让控制器保存 descriptionphase 的 id进入我的联接 table,它基本上只有 3 列,id / description_id / phase_id。可能会有更多的元数据。

这是我的代码结构:

在说明中 Table:

$this->belongsToMany('Phases', [
        'foreignKey' => 'description_id',
        'targetForeignKey' => 'phase_id',
        'through' => 'DescriptionsPhases',
        'saveStrategy' => 'append'
    ]);

分阶段 Table:

$this->belongsToMany('Descriptions', [
        'foreignKey' => 'phase_id',
        'targetForeignKey' => 'description_id',
        'through' => 'DescriptionsPhases'
    ]);

在描述阶段 Table:

$this->belongsTo('Descriptions', [
        'foreignKey' => 'description_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Phases', [
        'foreignKey' => 'phases_id',
        'joinType' => 'INNER'
    ]);

在以上所有 3 个的实体中(目前):

protected $_accessible = [
    '*' => true
];

在描述控制器中,add() 方法。 $descriptions->phases 的 id 现在是硬编码的,只是为了降低复杂性:

public function add() {

    $description = $this->Descriptions->newEntity([
            'associated' => ['Phases']]);

    if ($this->request->is('post')) {

        // additional data to be saved in new description
        $description->user_id = $this->Auth->user('id');
        $description->designvariant = 666;

        // Hardcoded integer TO BE SAVED in associated model (via join table)
        $description->phases = [['id' => 5]]; 

        $patch = $this->Descriptions->patchEntity($description, $this->request->getData(), [
            'associated' => ['Phases']]);
        debug($patch);


        $save = $this->Descriptions->save($patch);
        debug($save);


        if ($save) {
            $this->Flash->success(__('The description has been saved.'));

            return $this->redirect(['action' => 'index']);

        }

    $this->Flash->error(__('The description could not be saved. Please, try again.'));
    }

    // pass arrays for dropdown menus
    $this->set('trades', ($this->Descriptions->Trades->listLabels()));
    $this->set('elements', ($this->Descriptions->Elements->listLabels()));
    $this->set('allocations', ($this->Descriptions->Allocations->listLabels()));

    $this->set(compact('description'));
    $this->render();
}

patchEntity和saveEntity我都调试过了,得到了这个数据结构。

调试($补丁);

object(App\Model\Entity\Description) {

'associated' => [
    (int) 0 => 'Phases'
],
'user_id' => (int) 1,
'designvariant' => (int) 666,
'phases' => [
    (int) 0 => [
        'id' => (int) 5
    ]
],
'element_id' => 'A',
'shorttext' => 'title',
'longtext' => 'text',
'trade_id' => '0',
'allocation_id' => 'BMST',
'[new]' => true,
'[accessible]' => [
    'trade_id' => true,
    'element_id' => true,
    'allocation_id' => true,
    'shorttext' => true,
    'longtext' => true,
    'designvariant_id' => true,
    'user_id' => true,
    'created' => true,
    'modified' => true,
    'trade' => true,
    'element' => true,
    'allocation' => true,
    'phase' => true,
    'phases' => true,
    'designvariant' => true,
    'user' => true,
    'amounts' => true,
    'costs' => true,
    'descriptions_phases' => true,
    'descriptions_phase' => true,
    '*' => true
],
'[dirty]' => [
    'associated' => true,
    'user_id' => true,
    'designvariant' => true,
    'phases' => true,
    'element_id' => true,
    'shorttext' => true,
    'longtext' => true,
    'trade_id' => true,
    'allocation_id' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Descriptions'

}

调试($保存);

object(App\Model\Entity\Description) {

'associated' => [
    (int) 0 => 'Phases'
],
'user_id' => (int) 1,
'designvariant' => (int) 666,
'phases' => [
    (int) 0 => [
        'id' => (int) 5
    ]
],
'element_id' => 'A',
'shorttext' => 'title',
'longtext' => 'text',
'trade_id' => '0',
'allocation_id' => 'BMST',
'created' => object(Cake\I18n\FrozenTime) {

    'time' => '2019-01-11T10:04:32+00:00',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'modified' => object(Cake\I18n\FrozenTime) {

    'time' => '2019-01-11T10:04:32+00:00',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'id' => (int) 133,
'[new]' => false,
'[accessible]' => [
    'trade_id' => true,
    'element_id' => true,
    'allocation_id' => true,
    'shorttext' => true,
    'longtext' => true,
    'designvariant_id' => true,
    'user_id' => true,
    'created' => true,
    'modified' => true,
    'trade' => true,
    'element' => true,
    'allocation' => true,
    'phase' => true,
    'phases' => true,
    'designvariant' => true,
    'user' => true,
    'amounts' => true,
    'costs' => true,
    'descriptions_phases' => true,
    'descriptions_phase' => true,
    '*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Descriptions'

}

记录顺利保存到描述中Table,但它不会保存任何关联数据。

我是不是遗漏了什么明显的东西? 任何帮助都将不胜感激。此外,如果您需要更多信息,请询问,因为这是我的第一个 post :-)

仅保存实体,即 phases 必须保存实体数组,而不是嵌套数组。嵌套数组是您要传递给 newEntity()/patchEntity() 的内容,它们将在其中相应地转换为实体。

$description->phases = [
    $this->Descriptions->Phases->get(5)
];

另见