+ All Categories
Home > Documents > ColdFusion 9 ORM

ColdFusion 9 ORM

Date post: 30-Dec-2015
Category:
Upload: dylan-potter
View: 49 times
Download: 1 times
Share this document with a friend
Description:
ColdFusion 9 ORM. Object Relational Mapping Overview Presented By: Denard Springle Northern Virginia ColdFusion Users Group. Advantages of ORM. Database Vendor Independence Caching & Concurrency Performance Optimization Faster Application Development - PowerPoint PPT Presentation
18
Object Relational Mapping Overview Presented By: Denard Springle Northern Virginia ColdFusion Users Group
Transcript

Object Relational Mapping Overview

Presented By: Denard SpringleNorthern Virginia ColdFusion Users Group

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

Cycles Inherent Object Oriented Programming

Application Development Framework

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

<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.

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

<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

book.cfc <cfcomponent persistent=“true”>

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

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)

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>

<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’.

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

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

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

<cfquery> DELETE <cfquery name=“qDelCustomer”

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

ORM <cfset EntityDelete(oCustomer)>

<cfset ormflush()>

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

<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.

<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.

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


Recommended