Skip to content

Instantly share code, notes, and snippets.

@KrunchMuffin
Created May 23, 2012 00:31
Show Gist options
  • Save KrunchMuffin/2772555 to your computer and use it in GitHub Desktop.
Save KrunchMuffin/2772555 to your computer and use it in GitHub Desktop.
cfwheels association help
<!--- pet model --->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<!--- relationships --->
<cfset hasOne(name="petProfile")>
<cfset hasMany(name="petDocuments", dependent="deleteAll")>
<cfset hasMany(name="personPets")>
<cfset hasMany(name="petEvents", dependent="deleteAll")>
<cfset hasMany(name="ViewPetEvents", foreignkey="petid")>
<cfset hasMany(name="petRoutineSchedules", dependent="deleteAll")>
<cfset belongsTo(name="breed", joinType="inner")>
<!--- nested properties --->
<cfset nestedProperties(associations="petProfile,petEvents", allowDelete=true)>
<!--- validations --->
<cfset validatesPresenceOf(property="pettype", when="onCreate")>
<!--- 3rd party --->
<cfset uploadableFile(property="image",nullWhenBlank=true)>
<!--- properties --->
<cfset property(name="breedId", label="Breed")>
<cfset property(name="name", label="Name")>
<cfset property(name="weight", label="Weight")>
<cfset property(name="isfixed", label="Spayed/Neutered")>
<cfset property(name="birthdate", label="Date of Birth")>
<cfset property(name="pettype", label="Pet Type")>
</cffunction>
</cfcomponent>
<!--- event model --->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<cfset hasMany(name="petRoutineSchedules", dependent="deleteAll")>
<cfset hasMany(name="petEvents", dependent="deleteAll")>
<cfset hasMany(name="viewPetEvents", dependent="deleteAll")>
</cffunction>
</cfcomponent>
<!---petprofile model --->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<cfset belongsTo(name="pet")>
</cffunction>
</cfcomponent>
<!--- petroutineschedule model--->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<cfset hasMany(name="pets", dependent="deleteAll")>
<cfset hasMany(name="events", dependent="deleteAll")>
</cffunction>
</cfcomponent>
<!--- petevent model --->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<cfset belongsTo(name="pet")>
<cfset belongsTo(name="event", joinType="outer")>
<cfset belongsTo(name="viewpetevent", foreignkey="eventid")>
</cffunction>
</cfcomponent>
<!--- ViewPetEvent model this is a db view--->
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false" access="public">
<cfset table("vw_petevent")>
<cfset setPrimaryKey("eventid")>
<cfset belongsTo(name="event",joinType="outer")>
<cfset belongsTo(name="pet",joinType="outer")>
</cffunction>
</cfcomponent>
<!--- controller call --->
<cfset userpets = model("personPets").findAllByPersonId(value=session.security.userid,
include="pet(petprofile,petdocuments,petroutineschedules,viewpetevents,petevents(event),breed(pettype))",
select="pets.id AS petid,name,birthdate,isfixed,gender,color,image,weight,breedname,pettype,vw_petevent.startdate,
vw_petevent.eventid,timeunit,dateunit,eventname",
order="name",
cache=false)>
<!--- Query I want to happen --->
<cfquery name="userpets" datasource="PetTrackr">
SELECT
pettypes.pettype,
breeds.breedname,
petroutineschedules.timeunit,
petroutineschedules.dateunit,
petdocuments.filename,
petdocuments.filelocation,
events.eventname,
events.eventnameenglish,
events.isroutine,
events.type,
pets.weight,
pets.image,
pets.color,
pets.gender,
pets.isfixed,
pets.birthdate,
pets.name,
pets.notes,
petprofiles.microchipnumber,
pets.id as petid
FROM
peoplepets
LEFT JOIN pets
ON peoplepets.petid = pets.id
LEFT JOIN petdocuments
ON petdocuments.petid = pets.id
LEFT JOIN petroutineschedules
ON petroutineschedules.petid = pets.id
LEFT JOIN events
ON petroutineschedules.eventid = events.id
LEFT JOIN vw_petevent
ON vw_petevent.eventid = events.id
INNER JOIN breeds
ON pets.breedid = breeds.id
INNER JOIN pettypes
ON breeds.pettypeid = pettypes.id
LEFT JOIN petprofiles
ON petprofiles.petid = pets.id
WHERE
peoplepets.personid = <cfqueryparam cfsqltype="cf_sql_integer" value="#session.security.userid#">
</cfquery>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment