如果这个问题得到解答,我会提前道歉,但我搜索得很彻底,找不到我要找的东西。
我的表单中包含多个具有相同名称的输入,但我无法将数据保存到数据库中。
下面是我的表单代码以及我的控制器代码。
提前致谢。 查理
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'));
}
}
}
}
答案 0 :(得分:1)
有一个名为saveMany的方法可以为同一个模型保存多行,信息在这里:
试试这个:
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'));
}
}
}
}