2

I am using ColdFusion ORM with FW/1.

I have two tables student and parent, I have given many-to-one relationship as we know parent can have more wards in a school. I am adding student and parent separately, while adding parent I need parentId to be stored in student table so that I can list or get student with their parents for further features and activity.

Can anyone help me out how should I save the parent in a parent table and also parentId in student table?

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"; 
}

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";


This is the way i am saving parent in my controllers:

<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')>

How can I save parentid in student table while saving parent? I mean how can i call student save method to have parentid in student table?

Any help would be appreciated.

thanks

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
suleman khan
  • 97
  • 1
  • 1
  • 9
  • If possible, please post some code showing 1.) the ORM config for both entities and 2.) the code you have so far for creating these records. – existdissolve Feb 02 '13 at 12:35
  • I don't know about ORM, but if you were writing your own queries, you would insert the parent record first, then the student record. – Dan Bracuk Feb 02 '13 at 14:32
  • yeah this can be possible if i insert parent record first then student i have to select parent in the form for parentId. but the requirement is first i have to add student then parent. – suleman khan Feb 02 '13 at 15:14
  • Where did the requirment of adding the student first come from? What if the parent is already in your db because they have another student? – Dan Bracuk Feb 02 '13 at 16:48
  • I agree with you but, at first we insert student then parent. – suleman khan Feb 02 '13 at 16:53
  • Why do you insert the student before the parent? – Dan Bracuk Feb 02 '13 at 17:33

1 Answers1

0

Try this

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

And try setting cascade="any" on the student property of Parent. This should allow you to save both objects...even if they are both are new objects being persisted for the first time.

Scott Stroz
  • 7,510
  • 2
  • 21
  • 25
  • thanks for your response now its working..i have to setparent in student object to get the parentId in the student table while saving parent. – suleman khan Feb 02 '13 at 17:08