2011-04-14 52 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'])); 
}