我在StackOverflow上找到了一些关于这个主题的非常有用的教程和帖子,但我完全被困在一点上。
除了我的HABTM Zip数据之外,以下所有内容都在使用。
这是我的代码:
<?php for ($i = 1; $i <= 3; $i++) { // 3 fields at a time ?>
<?php echo $this->Form->input('Plan.' . $i . '.plan_detail_id', array(
'options' => $plans_list,
'type' => 'select',
'empty' => '-- Select a the Plan Detail --',
'label' => 'Select a the Plan Detail'
)); ?>
<?php echo $this->Form->input('Plan.' . $i . '.monthly_cost', array('label' => 'Monthly Cost')); ?>
<?php echo $this->Form->input('Plan.' . $i . '.dental_cost', array('label' => 'Dental Cost')); ?>
<?php echo $this->Form->input('Plan.' . $i . '.age_id', array('label' => 'Select an Age Range', 'empty' => '-- Select an Age Range --')); ?>
<?php echo $this->Form->input('Plan.' . $i . '.applicant_id', array('label' => 'Applicant Type', 'empty' => '-- Select an Applicant Type --')); ?>
<?php echo $this->Form->input('Plan.' . $i . '.state_id', array('label' => 'Select a State', 'empty' => '-- Select a State --')); ?>
<?php echo $this->Form->input('"$i".Zip', array(
'type' => 'select',
'multiple' => 'true',
'label' => 'Select the Zips'
));
<?php } // end for() ?>
我的控制器操作如下:
function bulk_add() {
if (!empty($this->data)) {
$this->Plan->create();
if ($this->Plan->saveAll($this->data, array('validate' => 'false'))) {
$this->Session->setFlash(__('The plan has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The plan could not be saved. Please, try again.', true));
}
}
$ages = $this->Plan->Age->find('list', array('order' => array('Age.name ASC')));
$applicants = $this->Plan->Applicant->find('list', array('order' => array('Applicant.name ASC')));
$states = $this->Plan->State->find('list', array('order' => array('State.name ASC')));
$zips = $this->Plan->Zip->find('list', array('order' => array('Zip.title ASC')));
$this->set(compact('planDetails', 'ages', 'applicants', 'states', 'zips'));
$plans_list = array();
$plans = $this->Plan->PlanDetail->find('all', array('order' => array('PlanDetail.name ASC')));
foreach ($plans as $row) {
$plans_list["{$row['PlanDetail']['id']}"] = "{$row['PlanDetail']['name']} - {$row['PlanDetailNote']['name']}";
}
$this->set('plans_list', $plans_list);
}
答案 0 :(得分:1)
第3天:)
我不能将我的数组(有多个条目)不能用数字索引。并且多个表上的saveAll()需要一个键控数组才能使其正常工作。
我在下面有一个完整的数据转储,带有数字索引数组,并且不知何故需要通过键索引(我可以让它正常工作,但仅限于单个记录插入)..
我对bulk_add的看法
<?php for ($i = 1; $i <= 2; $i++) { ?>
<table>
<tr>
<td><?php echo $this->Form->input("$i.plan_detail_id", array(
'options' => $plans_list,
'type' => 'select',
'empty' => '-- Select a the Plan Detail --',
'label' => 'Select a the Plan Detail'
));
?></td>
<td><?php echo $this->Form->input("$i.monthly_cost", array('label' => 'Monthly Cost')); ?></td>
<td><?php echo $this->Form->input("$i.dental_cost", array('label' => 'Vision Cost')); ?></td>
<td><?php echo $this->Form->input("$i.age_id", array('label' => 'Select an Age Range', 'empty' => '-- Select an Age Range --')); ?></td>
<td><?php echo $this->Form->input("$i.applicant_id", array('label' => 'Applicant Type', 'empty' => '-- Select an Applicant Type --')); ?></td>
<td><?php echo $this->Form->input("$i.state_id", array('label' => 'Select a State', 'empty' => '-- Select a State --')); ?></td>
<td>
<?php echo $this->Form->input("$i.Zip", array('multiple' => 'true')); ?>
</td>
</tr>
</table>
<?php } // end for() ?>
<?php
echo $this->Form->end(__('Submit', true));
?>
我的控制器代码:
function bulk_add() {
if (!empty($this->data)) {
$this->Plan->create();
if ($this->Plan->saveAll($this->data, array('atomic' => 'false'))) {
// Debug
debug($this->data);
$this->Session->setFlash(__('The plan has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The plan could not be saved. Please, try again.', true));
}
}
$ages = $this->Plan->Age->find('list', array('order' => array('Age.name ASC')));
$applicants = $this->Plan->Applicant->find('list', array('order' => array('Applicant.name ASC')));
$states = $this->Plan->State->find('list', array('order' => array('State.name ASC')));
$zips = $this->Plan->Zip->find('list', array('order' => array('Zip.title ASC')));
$this->set(compact('planDetails', 'ages', 'applicants', 'states', 'zips'));
$plans_list = array();
$plans = $this->Plan->PlanDetail->find('all', array('order' => array('PlanDetail.name ASC')));
foreach ($plans as $row) {
$plans_list["{$row['PlanDetail']['id']}"] = "{$row['PlanDetail']['name']} - {$row['PlanDetailNote']['name']}";
}
$this->set('plans_list', $plans_list);
}
这是调试转储:
Array
(
[Plan] => Array
(
[1] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 14
[applicant_id] => 1
[state_id] => 1
)
[2] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 2
[applicant_id] => 4
[state_id] => 1
)
)
[1] => Array
(
[1] => Array
(
[Zip] => Array
(
[0] => 487
[1] => 486
[2] => 485
[3] => 484
[4] => 483
)
)
)
[2] => Array
(
[2] => Array
(
[Zip] => Array
(
[0] => 485
[1] => 484
[2] => 483
)
)
)
)
答案 1 :(得分:0)
$这 - &GT;形状配合&GT;输入( '邮编');它在书中
查找找到('list')给你很好的foreach hack。
答案 2 :(得分:0)
如果要保存许多行,看起来您的输入名称错误。你可能想要“$ i.Plan.field”和“$ i.Zip”