CakePHP - 创建一个编辑同一模型的多行的表单 [英] CakePHP - Create a form which edits multiple rows of the same model

查看:87
本文介绍了CakePHP - 创建一个编辑同一模型的多行的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建一个由Subjects组成的表单,它属于SubjectGroups。在视觉上,主题在页面上显示在每个主题组的标题下。您可以编辑主题群组或个别主题的名称。

I am trying to build a form that consists of Subjects, which belong to SubjectGroups. Visually, the subjects are shown on the page under headings of each subject group. You will be able to edit the name of a subject group or an individual subject.

如果我给您一个HTML示例:

If I were to give you an example of the HTML:

<div class="heading">
    <input type="text" value="Subject Group 1" />
</div>
<input type="text" value="Subject 1" />
<input type="text" value="Subject 2" />
<input type="text" value="Subject 3" />

<div class="heading">
    <input type="text" value="Subject Group 2" />
</div>
<input type="text" value="Subject 4" />
<input type="text" value="Subject 5" />
<input type="text" value="Subject 6" />

...




  1. 如何使用Cake的FormHelper构建表单,这将允许我更新这样的多个行?

  1. How do I build the form using Cake's FormHelper that will allow me to update multiple rows like this?

如何验证和更新SubjectGroup和Subject模型?

How do I then validate and update both the SubjectGroup and Subject models?

如何处理每个模型的多个实例(主题1,主题2等)?

How do I process many instances of each model (subject 1, subject 2, etc.)?


推荐答案

请参阅保存相关模型数据(特别是Company hasMany帐户示例)。你应该可以通过循环你的数据来实现这一点:

See the documentation for Saving Related Model Data (specifically the numeric-syntax used in the "Company hasMany Account" example). You should be able to achieve this by looping over your data:

$i = $j = 0;
foreach ($subjectGroups as $subjectGroup):
    echo $this->Form->input('SubjectGroup.' . $i . '.id');
    echo $this->Form->input('SubjectGroup.' . $i . '.name');
    foreach ($subjectGroup['Subject'] as $subject):
        echo $this->Form->input('Subject.' . $j . '.id');
        echo $this->Form->input('Subject.' . $j . '.name');
        $j++;
    endforeach;
    $i++;
endforeach;

至于保存,您只需执行 $ this-> SubjectGroup-> ; saveAll($ this-> data);

As for saving, you just do $this->SubjectGroup->saveAll($this->data);.

这篇关于CakePHP - 创建一个编辑同一模型的多行的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆