2014-09-24 89 views
0

如果已經回答了,但我提前表示歉意,但是我徹底搜索並找不到要查找的內容。Cakephp saveAll不使用相同的字段名稱保存多個字段

我有一個具有相同名稱的多個輸入的表單,但我無法將數據保存到數據庫中。

下面是我的表單代碼以及我的控制器代碼。

在此先感謝。 查理

register_your_card.ctp

    echo $this->Form->input('RewardUser.0.reward_card_number', array('label' => '*Card number', 'class' => 'multipleInputs', 'maxlength' => '4')); 
       echo $this->Form->input('RewardUser.1.reward_card_number', array('label' => '', 'class' => 'multipleInputs', 'maxlength' => '4', 'div' => false)); 
       echo $this->Form->input('RewardUser.2.reward_card_number', array('label' => '', 'class' => 'multipleInputs', 'maxlength' => '4', 'div' => false)); 
       echo $this->Form->input('RewardUser.3.reward_card_number', array('label' => '', 'class' => 'multipleInputs', 'maxlength' => '7', 'div' => false)); 

RewardUsersController.php

public function register_your_card() { 
    $this->set('title_for_layout', 'Rewards'); 
    $this->set('meta_keywords', 'Rewards'); 
    $this->set('meta_description', 'Rewards');     

    if ($this->request->is('post')) { 

     if (!empty($this->request->data)) { 

      $this->RewardUser->create(); = 
      if ($this->RewardUser->saveAll($this->request->data['RewardUser'])) { 
       $lastInsertID = $this->RewardUser->getLastInsertID(); 
       $this->Session->write('currentUser', $lastInsertID); 
       return $this->redirect(array('controller' => 'rewardusers', 'action' => 'register_your_card_confirmation')); 
      } 
     } 
    } 
} 

回答

1

有一個名爲saveMany方法,對於同一型號節省多行,資訊:

http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array

試試這個:

public function register_your_card() { 
    $this->set('title_for_layout', 'Rewards'); 
    $this->set('meta_keywords', 'Rewards'); 
    $this->set('meta_description', 'Rewards');     

    if ($this->request->is('post')) { 

     if (!empty($this->request->data)) { 

      if ($this->RewardUser->saveMany($this->request->data['RewardUser'])) { 
       $lastInsertID = $this->RewardUser->getLastInsertID(); 
       $this->Session->write('currentUser', $lastInsertID); 
       return $this->redirect(array('controller' => 'rewardusers', 'action' => 'register_your_card_confirmation')); 
      } 
     } 
    } 
} 
+0

CakePHP 1.2中有「saveMany」嗎? – 2017-07-07 20:49:34

相關問題