简体   繁体   English

在Cakephp中保存post hasManth学生habtm主题

[英]Saving Post hasMany Student habtm Subject in Cakephp

I'm trying to save 3 tables of data from PostController and their relationships are as follow: 我正在尝试从PostController保存3个数据表,它们的关系如下:

Post hasMany Student
Student hasAndBelongsToMany Subject

In posts/ I have a multistep form, in the first step of which I have these inputs to save students: posts/我有一个多步骤表单,在第一步中,我有以下输入内容可以保存学生:

$this->Form->input('Student.0.name');
$this->Form->input('Student.1.name'); //an additional field that maybe added by the user

In the last step, I count the number of students input by the user in step 1 and then output the inputs for subjects accordingly like this: 在最后一步中,我计算了用户在步骤1中输入的学生人数,然后相应地输出学科的输入,如下所示:

$this->Form->input('Subject.0.name'); //Subject(s) for student 1
$this->Form->input('Subject.1.name'); //Subject(s) for student 2

But I don't know how to set the field names in Cakephp to save the subjects for each students in Post model. 但是我不知道如何在Cakephp中设置字段名称来保存Post模型中每个学生的主题。

I figured it out: 我想到了:

foreach ($this->session->data['Student'] as $key => $value) {
    $val = 'Student.' . $key . '.Subject';
    echo $this->Form->input($val);
}

The field names for subject inputs (submitting from Post model) should be: Student.0.Subject . subject输入(从Post模型提交)的字段名称应为: Student.0.Subject And I output the inputs using foreach loop so it's ready for any number of fields added by the user, and each fields correspond to respective student. 我使用foreach循环输出输入,因此可以为用户添加的任意数量的字段做好准备,并且每个字段对应于各自的学生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM