Skip to content

Instantly share code, notes, and snippets.

@mhenke
Created September 12, 2010 23:49
Show Gist options
  • Save mhenke/576624 to your computer and use it in GitHub Desktop.
Save mhenke/576624 to your computer and use it in GitHub Desktop.
This represents the sample code for the example of Many to Many
http://cfwheels.org/docs/1-1/function/hasmany
see many-to-many.cfm for example code used in hasmany doc
<cfcomponent extends="Model" output="false">
<cffunction name="init">
<cfset hasMany(name="Enrollments", shortcut="Students")>
</cffunction>
</cfcomponent>
CREATE TABLE 'litepost'.'Courses' (
'id' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
'course_name' VARCHAR(45) NOT NULL,
'instructor_name' VARCHAR(45),
'location' VARCHAR(45),
PRIMARY KEY ('id')
);
CREATE TABLE 'litepost'.'Students' (
'id' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
'student_name' VARCHAR(45) NOT NULL,
'student_major' VARCHAR(45),
PRIMARY KEY ('id')
);
CREATE TABLE 'litepost'.'Enrollments' (
'id' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
'studentID' INTEGER UNSIGNED NOT NULL,
'courseid' INTEGER UNSIGNED NOT NULL,
'grade' VARCHAR(45),
PRIMARY KEY ('id')
);
insert into students
(student_name, student_major)
values ('Mike','Management Information Systems');
insert into students
(student_name, student_major)
values ('Steve','Music');
insert into students
(student_name, student_major)
values ('Jeff','Industrial Technology');
insert into courses
(course_name, instructor_name, location)
values ('Statistics','Hendricks', 'TJ Majors 100');
insert into courses
(course_name, instructor_name, location)
values ('Macro Economics','Snyder', 'TJ Majors 100');
insert into courses
(course_name, instructor_name, location)
values ('Swimming','Snyder', 'Gym');
insert into courses
(course_name, instructor_name, location)
values ('Swimming','Snyder', 'Gym');
insert into enrollments
(studentID, courseid, grade)
values (1,3, 'A');
insert into enrollments
(studentID, courseid, grade)
values (1,2, 'C');
insert into enrollments
(studentID, courseid, grade)
values (2,2, 'B');
insert into enrollments
(studentID, courseid, grade)
values (3,2, 'B');
insert into enrollments
(studentID, courseid, grade)
values (3,3, 'C');
<cfcomponent extends="Model" output="false">
<cffunction name="init">
<cfset belongsTo("Student")>
<cfset belongsTo("Course")>
</cffunction>
</cfcomponent>
<!---
Example 1 & 2 Specify that instances of a Blog Entry model has many Blog comments.
--->
<!--- Example1: (The table for the associated model, not the current, should have the foreign key set on it.) --->
<!--- In the models/Entry `init()` method --->
<cfset hasMany("comments")>
<!--- Example 2: Automatically delete all associated `comments` whenever this object is deleted --->
!--- In the models/Entry `init()` method --->
<cfset hasMany(name="comments", dependent="deleteAll")>
<!---
Example 3, 4, & 5 Specify that an Student can have more than one Course,
and a Course can have more than one students.
--->
<!---
Example 3: Specify that a Student has many Enrollments and setup a shortcut to the `Course` model.
(Useful when dealing with many-to-many relationships.)
--->
<!--- In the models/Student `init()` method --->
<cfset hasMany(name="Enrollments", shortcut="Courses")>
<!--- In the models/Course `init()` method --->
<cfset hasMany(name="Enrollments", shortcut="Students")>
<!--- In the models/Enrollment `init()` method --->
<cfset belongsTo("Student")>
<cfset belongsTo("Course")>
<!---
Example 4: When not following Wheels naming conventions for associations, it can get complex to define how a `shortcut` works.
In this example, we are naming our `shortcut` differently than the actual model's name.
--->
<!--- In the models/Student.cfc `init()` method --->
<cfset hasMany(name="Enrollments", shortcut="sessions", through="Courses,Enrollments")>
<h1>Student model</h1>
<cfdump var="#student#">
<h1>Student model with Courses</h1>
<cfdump var="#student.courses()#">
<!--- Example 4
<h1>Student model with Sessions</h1>
<cfdump var="#student.sessions()#">
--->
<h1>Course model</h1>
<cfdump var="#course#">
<h1>Course model with Students</h1>
<cfdump var="#course.students()#">
<cfcomponent extends="Model" output="false">
<cffunction name="init">
<cfset hasMany(name="Enrollments", shortcut="Courses")>
<!--- Example 4
<cfset hasMany(name="Enrollments", shortcut="sessions", through="Enrollments,Courses")>
--->
</cffunction>
</cfcomponent>
<cfcomponent extends="Controller" output="false">
<cffunction name="show">
<cfset student = model("Student").findByKey(key=1, include="Enrollments") />
<cfset course = model("Course").findByKey(key=2, include="Enrollments") />
</cffunction>
</cfcomponent >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment