CakePHP - 多行编辑

时间:2011-04-14 00:11:51

标签: cakephp

如何对多行的表格进行编辑? 我按照这里的教程http://matsimitsu.com/blog/2008/01/06/saveall-with-cakephp.html进行了操作,但似乎没有用。

这就是我正在做的事情,但它不起作用。

谢谢,
三通

function editAll() {
    $this->data = $this->Settings->find('all', array('conditions' => $conditions));
}

然后在视图中,这就是我所拥有的

foreach ($this->data as $setting):
    echo $form->input('Setting.' . $setting['Setting']["id"] . '.value', array('value' => $setting['Setting']["value"]));
endforeach;

然后在我有的添加功能

function add() {
    if (!empty($this->data)) {
        $this->Setting->create();
        if ($this->Setting->saveAll($this->data)) {
            $this->Session->setFlash(__('The Setting has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The Setting could not be saved. Please, try again.', true));
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您需要包含id字段,因此控制器中的数据将如下所示:

'Setting' => array(
    0 => array(
        'id' => 42,
        'value' => 'foo'
    ),
    1 => array(…)
)

所以在视图中,请执行以下操作:

foreach ($this->data as $i => $setting) {
    echo $this->Form->hidden("Setting.$i.id", array('value' => $setting['Setting']['id']));
    echo $this->Form->input("Setting.$i.value", array('value' => $setting['Setting']['value']));
}