Skip to content

Instantly share code, notes, and snippets.

@bennadel
Last active March 22, 2020 10:32
Show Gist options
  • Save bennadel/9759150 to your computer and use it in GitHub Desktop.
Save bennadel/9759150 to your computer and use it in GitHub Desktop.
Learning ColdFusion 9: From SQL To ORM - A Conceptual Shift In Relationships
SELECT
c.id,
c.name,
ci.phone,
ci.email
FROM
contact c
INNER JOIN
contact_information ci
ON
c.contact_information_id = ci.id
<!---
Enable ORM for this component and persist the data
in the contact data table.
--->
<cfcomponent
output="false"
hint="I am a contact."
persistent="true"
table="contact">
<cfproperty
name="id"
type="numeric"
setter="false"
persistent="true"
fieldtype="id"
column="id"
ormtype="integer"
generator="identity"
length="10"
notnull="true"
/>
<cfproperty
name="name"
type="string"
persistent="true"
fieldtype="column"
column="name"
ormtype="string"
length="50"
notnull="true"
/>
<!---
The phone property is going to be spread across the
contact_information table. It will be joined on the
column, contact_information_id. Use the Table and
JoinColumn attributes to define this distributed
persistence model.
--->
<cfproperty
name="phone"
type="string"
default=""
persistent="true"
table="contact_information"
joincolumn="contact_information_id"
fieldtype="column"
column="phone"
length="30"
notnull="true"
/>
</cfcomponent>
<!---
Enable ORM for this component and persist the data
in the company data table.
--->
<cfcomponent
output="false"
hint="I am a company."
persistent="true"
table="company">
<cfproperty
name="id"
type="numeric"
setter="false"
persistent="true"
fieldtype="id"
column="id"
ormtype="integer"
generator="identity"
length="10"
notnull="true"
/>
<cfproperty
name="name"
type="string"
persistent="true"
fieldtype="column"
column="name"
ormtype="string"
length="50"
notnull="true"
/>
<!---
The phone property is going to be spread across the
contact_information table. It will be joined on the
column, contact_information_id. Use the Table and
JoinColumn attributes to define this distributed
persistence model.
--->
<cfproperty
name="phone"
type="string"
default=""
persistent="true"
table="contact_information"
joincolumn="contact_information_id"
fieldtype="column"
column="phone"
length="30"
notnull="true"
/>
</cfcomponent>
<!---
Enable ORM for this component and persist the data
in the contact_information data table.
--->
<cfcomponent
output="false"
hint="I am contact information."
persistent="true"
table="contact_information">
<cfproperty
name="id"
type="numeric"
setter="false"
persistent="true"
fieldtype="id"
column="id"
ormtype="integer"
generator="identity"
length="10"
notnull="true"
/>
<cfproperty
name="phone"
type="string"
persistent="true"
fieldtype="column"
column="phone"
ormtype="string"
length="30"
notnull="true"
/>
</cfcomponent>
<!---
Enable ORM for this component and persist the data
in the contact data table.
--->
<cfcomponent
output="false"
hint="I am a contact."
persistent="true"
table="contact">
<cfproperty
name="id"
type="numeric"
setter="false"
persistent="true"
fieldtype="id"
column="id"
ormtype="integer"
generator="identity"
length="10"
notnull="true"
/>
<cfproperty
name="name"
type="string"
persistent="true"
fieldtype="column"
column="name"
ormtype="string"
length="50"
notnull="true"
/>
<!---
This Contact contains a ContactInformation entity in a
one-to-one relationship. In this case, the foreign key
column (FKColumn) in THIS table is contact_information_id
and references the primary key of the contact_information
table, which is ID.
--->
<cfproperty
name="contactInformation"
type="any"
persistent="true"
fieldtype="one-to-one"
cfc="ContactInformation"
fkcolumn="contact_information_id"
fetch="join"
notnull="true"
cascade="all"
/>
</cfcomponent>
<!--- Create a new contact instance. --->
<cfset contact = new com.Contact() />
<!--- Create a new contact information instance. --->
<cfset contactInfo = new com.ContactInformation() />
<!--- Store the contact values. --->
<cfset contact.setName( "Tricia" ) />
<!--- Set the contact information. --->
<cfset contactInfo.setPhone( "212-555-7718" ) />
<!--- Store the contact information in the contact. --->
<cfset contact.setContactInformation( contactInfo ) />
<!--- Save the contact. --->
<cfset entitySave( contact ) />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment