2011-04-14 55 views
1

如何编辑多行的表格?
我跟着这里的教程http://matsimitsu.com/blog/2008/01/06/saveall-with-cakephp.html,但它似乎并没有工作。CakePHP - 多行编辑

这是我正在做的,但它不工作。

感谢,
三通

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

回答

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