更新_joinData中的值有效,但缺少正确的索引

时间:2018-08-27 16:47:45

标签: cakephp cakephp-3.0

经过大量工作,我终于找到了如何更新联接表的方法,但是我发现我绝对无法解决的问题。我手动添加了一组复选框来更新联接表上的项目,并且只要它们是我的旧数据库的一部分,它们就可以完美地工作。当我向ID较低的联接表中添加新项时,联接表上的错误项将被更新。

这是我的控制者:

public function edit($id)
{

    $brigada = $this->Brigadas
        ->findById($id)
        ->contain(['Tags', 'Voluntarios'])
        ->firstOrFail();

    if ($this->request->is(['patch', 'post', 'put'])) {
        // https://github.com/cakephp/cakephp/issues/7864
        if (!empty($this->request->data['voluntarios']['_ids'])) {
            $vol = [];
            $ids = [];
            foreach ($this->request->data['voluntarios']['_ids'] as $key=>$locationId) {

                $vol['_ids'][$key] = $locationId;

                $vol[] = [
                    '_joinData' => [
                        'lider' => isset($this->request->data['voluntarios'][$key]['_joinData']['lider']) ? $this->request->data['voluntarios'][$key]['_joinData']['lider'] : false
                    ]
                ];


            }
        }

        if (isset($vol)){                
            $this->request->data['voluntarios'] = $vol;
        }

        $this->Brigadas->patchEntity($brigada, $this->request->data, ['associated' => ['Voluntarios._joinData', 'Participaciones', 'Participaciones._joinData']]);

        if ($this->Brigadas->save($brigada)) {
            $this->Flash->success(__('Your team data has been updated.'));
            return $this->redirect(['action' => 'view', $id]);
        }
        $this->Flash->error(__('Unable to update your team data.'));
    }
}

这就是我迭代表单的方式。注意,我为id添加了一个额外的隐藏表单,只是为了尝试更改id号。但是结果最终是完全一样的。

    foreach($all_vol as $key=>$voluntario){
        echo $this->Form->control('voluntarios.'.$key.'.id', ['hidden' => true]);
        echo $this->Form->control('voluntarios.'.$key.'._joinData.lider', [
            'label' => $voluntario->full_name,
            'default'=>'0',
        ]);
    }

这是表单发送的请求。

[
    '_ids' => [
        (int) 0 => '1',
        (int) 1 => '2',
        (int) 2 => '184',
        (int) 3 => '303',
        (int) 4 => '1838',
        (int) 5 => '1873',
        (int) 6 => '2553',
        (int) 7 => '4467',
        (int) 8 => '4973',
        (int) 9 => '5148',
        (int) 10 => '5493',
        (int) 11 => '6307',
        (int) 12 => '7055',
        (int) 13 => '7467',
        (int) 14 => '7561',
        (int) 15 => '7567',
        (int) 16 => '7864'
    ],
    (int) 0 => [
        'id' => '184',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 1 => [
        'id' => '303',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 2 => [
        'id' => '1838',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 3 => [
        'id' => '1873',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 4 => [
        'id' => '2553',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 5 => [
        'id' => '4467',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 6 => [
        'id' => '4973',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 7 => [
        'id' => '5148',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 8 => [
        'id' => '5493',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 9 => [
        'id' => '6307',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 10 => [
        'id' => '7055',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 11 => [
        'id' => '7467',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 12 => [
        'id' => '7561',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 13 => [
        'id' => '7567',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 14 => [
        'id' => '7864',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 15 => [
        'id' => '1',
        '_joinData' => [
            'lider' => '1'
        ]
    ],
    (int) 16 => [
        'id' => '2',
        '_joinData' => [
            'lider' => '1'
        ]
    ]
]

我在底部添加了ID为1和2的项目,并将它们变成了“ lider”,但是当它们传递给patchEntity时,两个不同的项目被选择为“ lider”。这是手动编辑后的请求。

[
    (int) 0 => [
        'id' => '1',
        '_joinData' => [
            'lider' => '1'
        ]
    ],
    (int) 1 => [
        'id' => '2',
        '_joinData' => [
            'lider' => '1'
        ]
    ],
    (int) 2 => [
        'id' => '184',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 3 => [
        'id' => '303',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 4 => [
        'id' => '1838',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 5 => [
        'id' => '1873',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 6 => [
        'id' => '2553',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 7 => [
        'id' => '4467',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 8 => [
        'id' => '4973',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 9 => [
        'id' => '5148',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 10 => [
        'id' => '5493',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 11 => [
        'id' => '6307',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 12 => [
        'id' => '7055',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 13 => [
        'id' => '7467',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 14 => [
        'id' => '7561',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 15 => [
        'id' => '7567',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    (int) 16 => [
        'id' => '7864',
        '_joinData' => [
            'lider' => '0'
        ]
    ],
    '_ids' => [
        (int) 0 => '1',
        (int) 1 => '2',
        (int) 2 => '184',
        (int) 3 => '303',
        (int) 4 => '1838',
        (int) 5 => '1873',
        (int) 6 => '2553',
        (int) 7 => '4467',
        (int) 8 => '4973',
        (int) 9 => '5148',
        (int) 10 => '5493',
        (int) 11 => '6307',
        (int) 12 => '7055',
        (int) 13 => '7467',
        (int) 14 => '7561',
        (int) 15 => '7567',
        (int) 16 => '7864'
    ]
]

最后,我是否手动编辑都没有关系,因为我是在我的patchEntity调用中得到的。请注意,“ id”既不是1也不是2:

    '_joinData' => object(App\Model\Entity\Participacione) {
        'id' => (int) 981,
        'brigada_id' => (int) 222,
        'voluntario_id' => (int) 7567,
        'lider' => true,
        '[new]' => false,
        '[accessible]' => [
            'brigada_id' => true,
            'voluntario_id' => true,
            'lider' => true,
            'brigada' => true,
            'voluntario' => true

我该如何解决?我已经发布了其他问题,但没有很多答案,但是我将继续对人类充满信心,以解决这个问题。预先感谢您阅读我的问题!

1 个答案:

答案 0 :(得分:0)

令人惊讶的是,我设法通过使用表中的sort属性来解决它。像这样:

$this->belongsToMany('Voluntarios', [
        'foreignKey' => 'brigada_id',
        'targetForeignKey' => 'voluntario_id',
        'joinTable' => 'participaciones',
        'saveStrategy' => 'replace',
        'through' => 'Participaciones',
        'sort' => ['Voluntarios.id' => 'ASC'],
    ]);

我觉得一切都很迫切,但是我找不到更好的方法。在删除加星标的值时,我注意到了一些错误,但这是朝着正确方向迈出的一步。