我正在使用带有 FW/1 的 ColdFusion ORM。
我有两张表学生和家长,我已经给出了多对一的关系,因为我们知道家长可以在学校有更多的病房。我分别添加学生和家长,在添加家长时,我需要将 parentId 存储在学生表中,以便我可以列出或让学生与他们的家长一起获得更多功能和活动。
任何人都可以帮助我如何将父母保存在父表中,并将 parentId 保存在学生表中吗?
学生.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";
}
父.cfc
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保存在学生表中?我的意思是我怎样才能调用学生保存方法在学生表中有父ID?
任何帮助,将不胜感激。
谢谢