2

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

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

So I have this /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>

And this add() in /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']);
    }

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.

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

ndm
  • 59,784
  • 9
  • 71
  • 110
eagers
  • 59
  • 2
  • 8

1 Answers1

1

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

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

Here is how it should be

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