如何将 id 和 value 从我在 cakePHP 中的输入表单传递给控制器​​?

How can I pass both id and value to the controller from my input form in cakePHP?

这是我的代码

查看(edit.ctp):

<?php echo $this->Form->create('Answer'); ?>
<?php echo $this->Form->input('Answer.0.value', array('class' => 'validate', 'type' => 'text', 'id' => 'Answer.0.id', 'label' => false)) ?>
<?php echo $this->Form->end('Edit Answer'); ?>

控制器:

public function edit($id) {
    $this->layout = 'request_processing_edit';
    $data = $this->Response->findById($id);
    if ($this->request->is(array('post', 'put'))) {
        $this->Response->id = $id;
        if ($this->Answer->save($this->request->data)) {
            $this->Session->setFlash('Answer Editted');
            $this->redirect('index');
        }
}

这是 $this->request->data 数组的样子:

I need the id to be in the same array as value upon clicking submit in the view

有什么方法可以实现这个而不必在控制器中构建数组吗?寻找一种方法,在单击 view/form.

提交后在请求中传递 id 和 value

通过在我的 ctp 文件中添加这行代码找到了解决方案:

<?php echo $this->Form->input('Answer.0.id', array('hidden' => true)) ?>

所以,这里可以使用这种方式

在你的控制器中:

$data = $this->Post->findById($id);

if ($this->request->is(array('post', 'put'))) {
    $this->Answer->id = $id;
    if ($this->Answer->save($this->request->data)) {
         $this->Session->setFlash('Answer Editted');
        return $this->redirect(array('action' => 'index'));
    }
     $this->Session->setFlash('Answer Not Editted');
}

if (!$this->request->data) {
    $this->request->data = $data;

在您的视图文件中传递隐藏字段 ID:

echo $this->Form->input('id', array('type' => 'hidden'));