Orm 使用多对一关系单独添加记录时,变量未定义

Orm 使用多对一关系单独添加记录时,变量未定义,orm,coldfusion,coldfusion-9,fw1,Orm,Coldfusion,Coldfusion 9,Fw1,我正在使用带有FW/1的ColdFusion表单 我有两张桌子,学生和家长,我给出了多对一的关系,因为我们知道家长在学校里可以有更多的病房。我将分别添加学生和家长,而添加家长时,我需要将parentId存储在学生表中,以便我可以列出或获取学生及其家长,以获得进一步的功能和活动 有人能帮我解决如何在父表中保存父项以及在学生表中保存父项ID的问题吗 student.cfc component output="false" persistent="true" accessors="true" enti

我正在使用带有FW/1的ColdFusion表单

我有两张桌子,学生和家长,我给出了多对一的关系,因为我们知道家长在学校里可以有更多的病房。我将分别添加学生和家长,而添加家长时,我需要将parentId存储在学生表中,以便我可以列出或获取学生及其家长,以获得进一步的功能和活动

有人能帮我解决如何在父表中保存父项以及在学生表中保存父项ID的问题吗

student.cfc

component output="false" persistent="true" accessors="true" entityname="Student" table="school_student" {


          // Use a mysql autonumber for an ID
          property name="StudentId" column="school_studentid" type=numeric fieldtype="id" generator="identity";
          property name="Fullname" column="school_studentFullname" type="string" length="128" notnull="true";
          property name="email" column="school_studentEmail" type="string" length="128" notnull="true";
          property name="password" column="school_studentPassword" type="string" length="64";

          property name="Parent" fieldtype="many-to-one" cfc="Parent" fkcolumn="student_school_parentId" lazy="true" singularname="Parent"; 
}
component output="false" accessors="true" persistent="true" entityname="Parent" table="school_parent" {


        //use mysql autonumber id
        property name="parentId" fieldtype="id" column="school_parentid" generator="identity";
        property name="name" type="string" column="school_parentname" length="128"; 
        property name="email" type="string" column="school_parentemail" length="128" ;
        property name="password" type="string" column="school_parentpassword" length="64";


        //relate parent with Student.
        property name="Student" fieldtype="one-to-many" cfc="Student" fkcolumn="student_school_parentId" lazy="extra" inverse="true";
parent.cfc

component output="false" persistent="true" accessors="true" entityname="Student" table="school_student" {


          // Use a mysql autonumber for an ID
          property name="StudentId" column="school_studentid" type=numeric fieldtype="id" generator="identity";
          property name="Fullname" column="school_studentFullname" type="string" length="128" notnull="true";
          property name="email" column="school_studentEmail" type="string" length="128" notnull="true";
          property name="password" column="school_studentPassword" type="string" length="64";

          property name="Parent" fieldtype="many-to-one" cfc="Parent" fkcolumn="student_school_parentId" lazy="true" singularname="Parent"; 
}
component output="false" accessors="true" persistent="true" entityname="Parent" table="school_parent" {


        //use mysql autonumber id
        property name="parentId" fieldtype="id" column="school_parentid" generator="identity";
        property name="name" type="string" column="school_parentname" length="128"; 
        property name="email" type="string" column="school_parentemail" length="128" ;
        property name="password" type="string" column="school_parentpassword" length="64";


        //relate parent with Student.
        property name="Student" fieldtype="one-to-many" cfc="Student" fkcolumn="student_school_parentId" lazy="extra" inverse="true";

这是我在控制器中保存父级的方式:

<cfargument name="rc" type="struct" required="true">
<cfset parent = getParentService().parent(arguments.rc.parentid)>
<cfset student = getStudentService().student(arguments.rc.studentId)>
<cfset parent.setName(arguments.rc.name)>
<cfset parent.setEmail(arguments.rc.email)>
<cfset parent.setPassword(arguments.rc.password)>
<cfset getParentService().save(parent)>
<cfset student.addparent(parent)>
<cfset variables.fw.redirect('parent.list')>

在保存家长时,如何在学生表中保存家长ID?我的意思是如何调用student save方法在student表中具有parentid

任何帮助都将不胜感激

谢谢你试试这个

<cfset parent.addstudent(student)>
<cfset getParentService().save(parent)>


然后尝试在父对象的student属性上设置cascade=“any”。这应该允许您保存这两个对象…即使它们都是第一次持久化的新对象。

如果可能,请发布一些代码,显示1.)两个实体的ORM配置和2.)到目前为止用于创建这些记录的代码。我不知道ORM,但如果您正在编写自己的查询,你可以先插入家长记录,然后插入学生记录。是的,如果我先插入家长记录,然后插入学生记录,这是可能的。我必须在表格中为parentId选择家长。但要求是先加学生,然后再加家长。先加学生的要求是从哪里来的?如果家长已经在您的数据库中,因为他们有另一个学生,该怎么办?我同意您的看法,但首先我们先插入学生,然后插入家长。感谢您的回答,现在它可以工作了。我必须在学生对象中设置家长,以便在保存家长时在学生表中获取家长ID。