【发布时间】:2011-07-12 05:14:45
【问题描述】:
我在 CakePHP 中保存多个条目时遇到了一些问题。我不确定saveAll() 是否会起作用,因为我正在尝试为从连接表中检查的每个项目保存一个条目,该连接表将两个单独的表连接到保存到的表中。
我有以下表格:
Samples、Results 和一个名为 SampleTypeTests 的连接表(连接两个单独的表 - SampleTypes 和测试)。
每个样本都有一个 FK sample_type_id
每个 Result 都有 FK sample_id, sample_type_test_id
我有一个“结果添加”视图,其中列出了 Samples.sample_type_id 的所有 SampleTypeTests
目前我的添加视图表单如下所示:
<?php
echo $this->Form->create('Result');
echo '<fieldset>';
echo '<legend>Choose Analysis</legend>';
echo $this->Form->input('sample_id', array('type'=>'hidden', 'value' => $this->passedArgs['0']));
echo $this->Form->input('sample_type_test_id',array(
'label' => __('Tests',true),
'type' => 'select',
'multiple' => 'checkbox',
'options' => $sampleTypeTests,
'selected' => $html->value('Result.sample_type_test_id'),
'hiddenField' => false
));
echo '</fieldset>';
echo $this->Form->end(__('Submit', true)); ?>
储蓄部分是我最头疼的地方。我需要为每个选中的SampleTypeTests 创建一个Results 条目,并将SampleTypeTest.id 添加到FK Results.sample_type_tests_id。
我已尝试根据我的需要调整以下内容http://mrphp.com.au/code/working-habtm-form-data-cakephp#habtm_checkbox
这是我得到的:
function add() {
if ($this->Result->saveAll($this->data['Result'])) {
$this->Session->setFlash(__('All required tests have been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Could not be saved. Please, try again.', true));
}
// Everything else
}
这没有奏效。以上可能是错误的。更多的是展示我的尝试。我迷失在这一切中,我需要一些帮助。我需要使用saveAll吗?我需要嵌套save 吗?我是否让它变得比需要的复杂得多?
更新:
我注意到上面的表单将数组输出为
Array (
[Result] => Array
(
[sample_id] => 21
[sample_type_test_id] => Array
(
[0] => 1
[1] => 24
[2] => 16
[3] => 71
)
)
)
它应该在什么时候产生:
Array
(
[Result] => Array(
[0] => Array
(
[sample_id] => 21
[sample_type_test_id] => 1
)
[1] => Array
(
[sample_id] => 21
[sample_type_test_id] => 24
)
)
)
那么我可以简单地使用saveAll($this->data['Result'])
关于如何改进表单以输出所需数组的任何提示?
【问题讨论】:
标签: cakephp save cakephp-1.3