如何在cakephp 3.x中保存关联的joinData [英] How to save associated joinData in cakephp 3.x

查看:75
本文介绍了如何在cakephp 3.x中保存关联的joinData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多对多关系中遇到问题和领域.联接表fields_problems有一个名为 fieldvalue 的字段.我正在尝试使用一种表单,该表单将问题记录以及多个记录插入到fields_problems中.

I have Problems and Fields in a many-to-many relationship. The join table fields_problems has a field named fieldvalue I am trying to have a form that will insert a problem record and also multiple records into fields_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'
    ]);
    }
...

/src/Model/Table/FieldsTable.php

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'
    ]);
    }
...

/src/Model/Table/FieldsProblemsTable.php

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'
    ]);
    }
...

我想添加一个新问题,将其链接到字段,然后将值添加到联接表的 fieldvalue 字段中.

And I want to Add a new problem, link it to fields, and add values to the fieldvalue field in the join table.

所以我有这个/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>

/src/Controller/ProblemsController.php

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']);
    }

当我填写并提交应用程序表单时,问题"记录将保存,但关联未保存,则没有任何内容插入fields_problems.

When I fill out and submit the app form, the Problem record is saved, but the association is not, nothing gets inserted into fields_problems.

我做错了什么,导致关联的joinData无法保存?

What am I doing wrong that is preventing the associated joinData from being saved?

推荐答案

尽管有食谱( http://book.cakephp.org/3.0/en/views/helpers/form.html )说要使用特殊的_ids键,不要!

Despite the cookbook (http://book.cakephp.org/3.0/en/views/helpers/form.html) saying to use the special _ids key, don't!

将"_ids"更改为"id"修复了表单,现在它可以正常地将数据保存到可合并的表单中了.

Changing "_ids" to "id" fixed the form and now it functions properly saving the data into the jointable.

这是我用来构建应用程序的食谱中的示例

Here is the example from the cookbook that I built my app with

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,
]);

这篇关于如何在cakephp 3.x中保存关联的joinData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆