Cakephp-saveAll不使用同一字段名保存多个字段


Cakephp saveAll not saving multiple fields using the same field name

如果这个问题已经得到回答,我提前道歉,但我搜索了一遍,找不到我要找的东西。

我有一个具有多个同名输入的表单,但我无法将数据保存到数据库中。

下面是我的表单代码以及控制器代码。

提前谢谢。Charlie

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

奖励用户控制器.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'));
            } 
        }
    } 
} 

有一个名为saveMany的方法可以为同一个模型保存多行,信息如下:

http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany数组数据null数组选项数组

试试这个:

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