CakePHP 为单独模型的每个选中项保存
我在 CakePHP 中保存多个条目时遇到一些问题。我不确定 saveAll() 是否能解决问题,因为我试图为从连接表中检查的每个项目保存一个条目,该连接表将两个单独的表连接到要保存的表。
I have the following tables:
Samples
, Results
and a join table called SampleTypeTests
(joins two separate tables - SampleTypes & Tests).每个样本都有一个 FK sample_type_id
每个结果都有 FK sample_id
、sample_type_test_id
我有一个结果的添加视图,其中列出了 Samples.sample_type_id
的所有 SampleTypeTests
code>
我的添加视图表单目前看起来像这样:
<?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
吗?我需要嵌套保存
吗?我是否让它变得比需要的更加复杂?
UPDATE:
我注意到上面的表单输出数组
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'])
关于如何改进的任何提示输出所需数组的形式?
I'm having some trouble saving multiple entries in CakePHP. I'm not sure whether saveAll()
will do the trick as I'm trying to save an entry for each item checked from a join table which joins two separate tables to the one being saved to.
I have the following tables:
Samples
, Results
and a join table called SampleTypeTests
(joins two separate tables - SampleTypes & Tests).Each Sample has an FK sample_type_id
Each Result has the FK sample_id
, sample_type_test_id
I have an Add view for Results which lists all SampleTypeTests
for a Samples.sample_type_id
My Add view form looks like this at the moment:
<?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)); ?>
The saving part is where I'm having the biggest headache. I need a Results
entry created for each SampleTypeTests
checked and that SampleTypeTest.id
to be added to the FK Results.sample_type_tests_id
.
I have tried to adapt the following to my needs http://mrphp.com.au/code/working-habtm-form-data-cakephp#habtm_checkbox
Here is what I got:
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
}
This hasn't worked. The above may be wrong. It is more to demonstrate what I've attempted. I'm getting very lost in it all and I need some help. Do I need to use saveAll
? Do I need to nest the save
? Have I made it far more complicated than it needs to be?
UPDATE:
I have noticed the above form outputs the array as
Array (
[Result] => Array
(
[sample_id] => 21
[sample_type_test_id] => Array
(
[0] => 1
[1] => 24
[2] => 16
[3] => 71
)
)
)
When what it should produce is:
Array
(
[Result] => Array(
[0] => Array
(
[sample_id] => 21
[sample_type_test_id] => 1
)
[1] => Array
(
[sample_id] => 21
[sample_type_test_id] => 24
)
)
)
Then I can simply use saveAll($this->data['Result'])
Any hints on how I can improve the form to output the desired array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚对另一篇可能对您有用的帖子给出了类似的答案,我现在没有时间修改它,但如果这不能回答您的问题,请告诉我,然后我会详细说明。
如何创建要保存的视图页面cakePHP 中的两个对象
好吧...在进一步阅读您的问题后,我认为这应该可以解决问题...
这应该保存到数据库中:
好的,然后试试这个...
I just gave a similar answer to another post that might be useful to you, I don't have time to modify it right now, but let me know if this doesn't answer your question then I will elaborate.
How to create a view page to save two objects in cakePHP
ok... after reading your problem further, I think this should do the trick...
This should save to the db as :
Ok try this then...