FW/1 で ColdFusion ORM を使用しています。
私は学生と親の 2 つのテーブルを持っています。親が学校でより多くの病棟を持つことができることを知っているので、多対 1 の関係を与えました。私は学生と親を別々に追加していますが、親を追加している間、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 を含めるにはどうすればよいですか?
どんな助けでも大歓迎です。
ありがとう