如何在cakephp 3.x中保存关联的joinData

如何在cakephp 3.x中保存关联的joinData,cakephp,orm,many-to-many,associations,cakephp-3.0,Cakephp,Orm,Many To Many,Associations,Cakephp 3.0,我在多对多的关系中有问题和领域。联接表fields\u problems有一个名为fieldvalue的字段。我正在尝试创建一个表单,该表单将在fields\u problems中插入一条问题记录和多条记录 /src/Model/Table/ProblemsTable.php class ProblemsTable extends Table { public function initialize(array $config) { parent::initialize(

我在多对多的关系中有问题和领域。联接表fields\u problems有一个名为fieldvalue的字段。我正在尝试创建一个表单,该表单将在fields\u problems中插入一条问题记录和多条记录

/src/Model/Table/ProblemsTable.php

class ProblemsTable extends Table
{
    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('problems');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsToMany('Fields', [
        'foreignKey' => 'problem_id',
        'targetForeignKey' => 'field_id',
        'joinTable' => 'fields_problems'
    ]);
    }
...
class FieldsTable extends Table
{

    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('fields');
    $this->displayField('name');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsToMany('Problems', [
        'foreignKey' => 'field_id',
        'targetForeignKey' => 'problem_id',
        'joinTable' => 'fields_problems'
    ]);
    }
...
class FieldsProblemsTable extends Table
{
    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('fields_problems');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->belongsTo('Fields', [
        'foreignKey' => 'field_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Problems', [
        'foreignKey' => 'problem_id',
        'joinType' => 'INNER'
    ]);
    }
...
public function add()
    {
        $problem = $this->Problems->newEntity();
        if ($this->request->is('post')) {
            $problem = $this->Problems->patchEntity($problem, $this->request->data, ['associated'=>['Fields._joinData']] );
            //$problem->dirty('fields',true);
            if ($this->Problems->save($problem)) {
                $this->Flash->success(__('The problem has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The problem could not be saved. Please, try again.'));
            }
        }
        $fields = $this->Problems->Fields->find('list', ['limit' => 200]);
        $this->set(compact('problem', 'fields'));
        $this->set('_serialize', ['problem']);
    }
/src/Model/Table/FieldsTable.php

class ProblemsTable extends Table
{
    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('problems');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsToMany('Fields', [
        'foreignKey' => 'problem_id',
        'targetForeignKey' => 'field_id',
        'joinTable' => 'fields_problems'
    ]);
    }
...
class FieldsTable extends Table
{

    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('fields');
    $this->displayField('name');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsToMany('Problems', [
        'foreignKey' => 'field_id',
        'targetForeignKey' => 'problem_id',
        'joinTable' => 'fields_problems'
    ]);
    }
...
class FieldsProblemsTable extends Table
{
    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('fields_problems');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->belongsTo('Fields', [
        'foreignKey' => 'field_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Problems', [
        'foreignKey' => 'problem_id',
        'joinType' => 'INNER'
    ]);
    }
...
public function add()
    {
        $problem = $this->Problems->newEntity();
        if ($this->request->is('post')) {
            $problem = $this->Problems->patchEntity($problem, $this->request->data, ['associated'=>['Fields._joinData']] );
            //$problem->dirty('fields',true);
            if ($this->Problems->save($problem)) {
                $this->Flash->success(__('The problem has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The problem could not be saved. Please, try again.'));
            }
        }
        $fields = $this->Problems->Fields->find('list', ['limit' => 200]);
        $this->set(compact('problem', 'fields'));
        $this->set('_serialize', ['problem']);
    }
/src/Model/Table/FieldsProblemsTable.php

class ProblemsTable extends Table
{
    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('problems');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsToMany('Fields', [
        'foreignKey' => 'problem_id',
        'targetForeignKey' => 'field_id',
        'joinTable' => 'fields_problems'
    ]);
    }
...
class FieldsTable extends Table
{

    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('fields');
    $this->displayField('name');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsToMany('Problems', [
        'foreignKey' => 'field_id',
        'targetForeignKey' => 'problem_id',
        'joinTable' => 'fields_problems'
    ]);
    }
...
class FieldsProblemsTable extends Table
{
    public function initialize(array $config)
    {
    parent::initialize($config);

    $this->table('fields_problems');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->belongsTo('Fields', [
        'foreignKey' => 'field_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Problems', [
        'foreignKey' => 'problem_id',
        'joinType' => 'INNER'
    ]);
    }
...
public function add()
    {
        $problem = $this->Problems->newEntity();
        if ($this->request->is('post')) {
            $problem = $this->Problems->patchEntity($problem, $this->request->data, ['associated'=>['Fields._joinData']] );
            //$problem->dirty('fields',true);
            if ($this->Problems->save($problem)) {
                $this->Flash->success(__('The problem has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The problem could not be saved. Please, try again.'));
            }
        }
        $fields = $this->Problems->Fields->find('list', ['limit' => 200]);
        $this->set(compact('problem', 'fields'));
        $this->set('_serialize', ['problem']);
    }
我想添加一个新问题,将其链接到字段,并向联接表中的fieldvalue字段添加值

所以我有这个/src/Template/Problems/add.ctp

<div class="problems form large-10 medium-9 columns">
    <?= $this->Form->create($problem) ?>
    <fieldset>
        <legend><?= __('Add Problem') ?></legend>
        <?php
            echo $this->Form->input("Problems.id");
            echo $this->Form->input('Problems.summary');

            echo $this->Form->input('Problems.Fields.0._ids', [
                'type' => 'select',
                'multiple' => false,
                'options' => $fields,
            ]);
            echo $this->Form->input('Problems.Fields.0._joinData.fieldvalue');

            echo $this->Form->input('Problems.Fields.1._ids', [
                'type' => 'select',
                'multiple' => false,
                'options' => $fields,
            ]);
            echo $this->Form->input('Problems.Fields.1._joinData.fieldvalue');
        ?>

    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>
当我填写并提交应用程序表单时,问题记录已保存,但关联未保存,没有任何内容插入到问题字段中

我做错了什么,阻止保存关联的joinData?

尽管食谱()说要使用特殊的_id键,但不要

将“_id”更改为“id”修复了表单,现在它可以正确地将数据保存到jointable中

下面是我构建应用程序时使用的食谱中的示例

echo $this->Form->input('tags.0.id');

echo $this->Form->input('tags._ids', [
    'type' => 'select',
    'multiple' => true,
    'options' => $tagList,
]);
这就是它应该是什么样子

echo $this->Form->input('tags.0.id', [
    'type' => 'select',
    'multiple' => false,
    'options' => $tagList,
]);