18
Object Relational Mapping Overview Presented By: Denard Springle Northern Virginia ColdFusion Users Group

ColdFusion ORM

Embed Size (px)

DESCRIPTION

This presentation was given during the NVCFUG OOP Code Camp sessions in May 2011 and covers basic ColdFusion 9 ORM topics (i.e. Intro to ORM).

Citation preview

Page 1: ColdFusion ORM

Object Relational Mapping Overview

Presented By: Denard SpringleNorthern Virginia ColdFusion Users Group

Page 2: ColdFusion ORM

Database Vendor Independence Caching & Concurrency Performance Optimization Faster Application Development Reduces or Eliminates Database Design

Cycles Inherent Object Oriented Programming

Application Development Framework

Page 3: ColdFusion ORM

DAO SQL Statements <cfquery>, <cfinsert>,

<cfupdate> Tables as Bean CFCs CRUD (Create, Retrieve,

Update, Delete) written for each object as DAO

Complex DAO relationships Increasingly complex code

management

ORM Persistent CFCs Inherent Getters & Setters Inherent Object Relational

Mapping Automates database

design Automates relationships Reduces Code Reduces Complexity

Page 4: ColdFusion ORM

<cfcomponent><cfset this.name = “MyApplication”><cfset this.ormenabled = “true”><cfset this.datasource = “MyDatasource”></cfcomponent>

Minimum required Application.cfc parameters for an ORM enabled application.

<cfset this.ormsettings = [struct]>

Defines additional settings that can be used to configure ORM.

Page 5: ColdFusion ORM

customer.cfc <cfcomponent persistent=“true”>

<cfproperty name=“id” generator=“increment”><cfproperty name=“name”><cfproperty name=“age”><cfproperty name=“email”></cfcomponent>

One <cfproperty> should be created for each column in the table

To specify a column mapping use column=“<column name>” in the <cfproperty> tag, otherwise the name is used as the column name

Page 6: ColdFusion ORM

<cfset aEx = EntityLoad(“customer”)>

Retrieves all records from the customer table.

<cfset oEx = EntityNew(“customer”)><cfset oEx.setname(“Jane Gum”)><cfset oEx.setage(“35”)><cfset oEx.setemail(“[email protected]”)><cfset EntitySave(oEx)><cfset ormflush()>

Creates new record and saves it to the customer table.

<cfset oEx = EntityLoad(“customer”, 1, true)><cfset oEx.setage(“45”)><cfset EntitySave(oEx)><cfset ormflush()>

Loads a record, sets a new age and saves it back to customer table.

<cfset EntityDelete(oEx)><cfset ormflush()>

Deletes the record from the customer table

Page 7: ColdFusion ORM

book.cfc <cfcomponent persistent=“true”>

<cfproperty name=“id” generator=“increment”><cfproperty name=“title”><cfproperty name=“author”></cfcomponent>

Page 8: ColdFusion ORM

customer.cfc <cfproperty name=“book_id” type=“array” fieldtype=“one-to-many”

cfc=“book” fkcolumn=“id”>

This defines a one-to-many relationship between a customer (one) and their books (many)

Page 9: ColdFusion ORM

customer.cfc <cfcomponent persistent=“true”>

<cfproperty name=“id” generator=“increment”><cfproperty name=“name”><cfproperty name=“age”><cfproperty name=“email”><cfproperty name=“book_id” type=“array” fieldtype=“one-to-many” cfc=“book” fkcolumn=“id”></cfcomponent>

Page 10: ColdFusion ORM

<cfset aC = EntityLoad(“customer”)>

Retrieves an array of all customer objects

<cfset oC = EntityLoad(“customer”, 27, true)>

Retrieves a customer object whose primary key value is 27.

<cfset aC = EntityLoad(“customer”, {age=“35”})>

Retrieves an array of customer objects whose age is 35.

<cfset aC = EntityLoad(“customer”, {age=“35”}, “name desc”)>

Retrieves an array of customer objects whose age is 35 sorted by name in descending order.

<cfset oC = EntityLoad(“customer”, {name=“Jane Gum”}, true)>

Retrieves a customer object for the customer whose name is ‘Jane Gum’.

Page 11: ColdFusion ORM

<cfquery> INSERT <cfquery name=“qPutCustomer”

datasource=“MyDatasource”>INSERT INTO customer (

name, age, email) VALUES (

<cfqueryparam value="Jane Gum" cfsqltype="cf_sql_varchar“>,<cfqueryparam value="35" cfsqltype="cf_sql_integer“>,<cfqueryparam value="[email protected]" cfsqltype="cf_sql_varchar"> </cfquery>

ORM <cfset oEx = EntityNew(“customer”)>

<cfset oEx.setname(“Jane Gum”)><cfset oEx.setage(“35”)><cfset oEx.setemail(“[email protected]”)><cfset EntitySave(oEx)><cfset ormflush()>

<cfscript>oEx = EntityNew(“customer”);oEx.setName(“Jane Gum”);oEx.setAge(“35”);oEx.setemail(“[email protected]”);EntitySave(oEx);ormflush();</cfscript>

Page 12: ColdFusion ORM

<cfquery> SELECT <cfquery name=“qGetCustomers”

datasource=“MyDatasource”>SELECT * FROM customer</cfquery>

<cfquery name=“qGetCustomer” datasource=“MyDatasource”>SELECT * FROM customerWHERE name = ‘Jane Gum’</cfquery>

ORM <cfset aCustomers =

EntityLoad(“customer”)>

<cfset oCustomer = EntityLoad(“customer”, {name=“Jane Gum”}, true)>

Page 13: ColdFusion ORM

<cfquery> UPDATE <cfquery name=“qUpdCustomer”

datasource=“MyDatasource”>UPDATE customer SETage = 45WHERE id = #qGetCustomer.id#</cfquery>

ORM <cfset oCustomer.setage(“45”)>

<cfset EntitySave(oCustomer)><cfset ormflush()>

Page 14: ColdFusion ORM

<cfquery> DELETE <cfquery name=“qDelCustomer”

datasource=“MyDatasource”>DELETE FROM customer WHERE id = #qGetCustomer.id#</cfquery>

ORM <cfset EntityDelete(oCustomer)>

<cfset ormflush()>

Page 15: ColdFusion ORM

<cfquery> Output <cfoutput>

#qGetCustomer.name#<br />#qGetCustomer.age#<br />#qGetCustomer.email#</cfoutput>

<cfset qGetCustomer.age = 45>

<cfoutput>#qGetCustomer.name#<br />#qGetCustomer.age#<br />#qGetCustomer.email#</cfoutput>

<cfquery name=“qUpdCustomer” datasource=“MyDatasource”>UPDATE customer SETage = #qGetCustomer.age#WHERE id = #qGetCustomer.id#</cfquery>

ORM Output <cfoutput>

#oCustomer.getname()#<br />#oCustomer.getage()#<br />#oCustomer.getemail()#</cfoutput>

<cfset oCustomer.setage(“45”)>

<cfoutput>#oCustomer.getname()#<br />#oCustomer.getage()#<br />#oCustomer.getemail()#</cfoutput>

<cfset EntitySave(oCustomer)><cfset ormflush()>

Page 16: ColdFusion ORM

<cfset aCustomers = EntityLoad(“customer”)><cfset qCustomers = EntityToQuery(aCustomers)>

Loads the customer object array and converts it to a query object.

<cfset aCustomers = EntityLoad(“customer”)><cfset qEmail = EntityToQuery(aCustomers, “email”)>

Loads the customer object array and converts only the ‘email’ column to a query object.

Page 17: ColdFusion ORM

<cfset aCustomers = ORMExecuteQuery(“from customer”)>

<cfset aCustomer = ORMExecuteQuery(“from customer where id = 27”, true>

<cfset aCustomer = ORMExecuteQuery(“from customer where name=:name”, {name=“Jane Gum”}, true>

<cfset aCustomers = ORMExecuteQuery(“from customer where age=:age and email=:email”, {age=35, email=“[email protected]”})>

Built-in functions to obtain data such as getage() and getname() cannot be used if you are using select queries with specific column names. The result will be returned as an array object and values can be retrieved using an array index.

Page 18: ColdFusion ORM

Chapter 8: Coldfusion ORM in the ‘Developing ColdFusion 9 Applications’ reference.

Adobe’s tutorials (available from the CF9 product page)

Ben Forta’s, Ray Camden’s, Dan Wilson’s, Ben Nadel’s (and others) blogs are always excellent references for all things ColdFusion

NVCFUG Adobe Groups Website