Your SlideShare is downloading. ×
0
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
ColdFusion ORM
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

ColdFusion ORM

966

Published on

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

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

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
966
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Object Relational Mapping Overview Presented By: Denard Springle Northern Virginia ColdFusion Users Group
  • 2. <ul><li>Database Vendor Independence </li></ul><ul><li>Caching & Concurrency </li></ul><ul><li>Performance Optimization </li></ul><ul><li>Faster Application Development </li></ul><ul><li>Reduces or Eliminates Database Design Cycles </li></ul><ul><li>Inherent Object Oriented Programming Application Development Framework </li></ul>
  • 3. <ul><li>DAO </li></ul><ul><li>SQL Statements </li></ul><ul><li><cfquery>, <cfinsert>, <cfupdate> </li></ul><ul><li>Tables as Bean CFCs </li></ul><ul><li>CRUD (Create, Retrieve, Update, Delete) written for each object as DAO </li></ul><ul><li>Complex DAO relationships </li></ul><ul><li>Increasingly complex code management </li></ul><ul><li>ORM </li></ul><ul><li>Persistent CFCs </li></ul><ul><li>Inherent Getters & Setters </li></ul><ul><li>Inherent Object Relational Mapping </li></ul><ul><li>Automates database design </li></ul><ul><li>Automates relationships </li></ul><ul><li>Reduces Code </li></ul><ul><li>Reduces Complexity </li></ul>
  • 4. <ul><li><cfcomponent> <cfset this.name = “MyApplication”> <cfset this.ormenabled = “true”> <cfset this.datasource = “MyDatasource”> </cfcomponent> Minimum required Application.cfc parameters for an ORM enabled application. </li></ul><ul><li><cfset this.ormsettings = [struct]> Defines additional settings that can be used to configure ORM. </li></ul>
  • 5. <ul><li>customer.cfc </li></ul><ul><li><cfcomponent persistent=“true”> <cfproperty name=“id” generator=“increment”> <cfproperty name=“name”> <cfproperty name=“age”> <cfproperty name=“email”> </cfcomponent> </li></ul><ul><li>One <cfproperty> should be created for each column in the table </li></ul><ul><li>To specify a column mapping use column=“<column name>” in the <cfproperty> tag, otherwise the name is used as the column name </li></ul>
  • 6. <ul><li><cfset aEx = EntityLoad(“customer”)> Retrieves all records from the customer table. </li></ul><ul><li><cfset oEx = EntityNew(“customer”)> <cfset oEx.setname(“Jane Gum”)> <cfset oEx.setage(“35”)> <cfset oEx.setemail(“nospam@nospam.com”)> <cfset EntitySave(oEx)> <cfset ormflush()> Creates new record and saves it to the customer table. </li></ul><ul><li><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. </li></ul><ul><li><cfset EntityDelete(oEx)> <cfset ormflush()> Deletes the record from the customer table </li></ul>
  • 7. <ul><li>book.cfc </li></ul><ul><li><cfcomponent persistent=“true”> <cfproperty name=“id” generator=“increment”> <cfproperty name=“title”> <cfproperty name=“author”> </cfcomponent> </li></ul>
  • 8. <ul><li>customer.cfc </li></ul><ul><li><cfproperty name=“book_id” type=“array” fieldtype=“one-to-many” cfc=“book” fkcolumn=“id”> </li></ul><ul><li>This defines a one-to-many relationship between a customer (one) and their books (many) </li></ul>
  • 9. <ul><li>customer.cfc </li></ul><ul><li><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> </li></ul>
  • 10. <ul><li><cfset aC = EntityLoad(“customer”)> Retrieves an array of all customer objects </li></ul><ul><li><cfset oC = EntityLoad(“customer”, 27, true)> Retrieves a customer object whose primary key value is 27. </li></ul><ul><li><cfset aC = EntityLoad(“customer”, {age=“35”})> Retrieves an array of customer objects whose age is 35. </li></ul><ul><li><cfset aC = EntityLoad(“customer”, {age=“35”}, “name desc”)> Retrieves an array of customer objects whose age is 35 sorted by name in descending order. </li></ul><ul><li><cfset oC = EntityLoad(“customer”, {name=“Jane Gum”}, true)> Retrieves a customer object for the customer whose name is ‘Jane Gum’. </li></ul>
  • 11. <ul><li><cfquery> INSERT </li></ul><ul><li><cfquery name=“qPutCustomer” datasource=“MyDatasource”> INSERT INTO customer ( name, age, email ) VALUES ( <cfqueryparam value=&quot;Jane Gum&quot; cfsqltype=&quot;cf_sql_varchar“>, <cfqueryparam value=&quot;35&quot; cfsqltype=&quot;cf_sql_integer“>, <cfqueryparam value=&quot;nospam@nospam.com&quot; cfsqltype=&quot;cf_sql_varchar&quot;> </cfquery> </li></ul><ul><li>ORM </li></ul><ul><li><cfset oEx = EntityNew(“customer”)> <cfset oEx.setname(“Jane Gum”)> <cfset oEx.setage(“35”)> <cfset oEx.setemail(“nospam@nospam.com”)> <cfset EntitySave(oEx)> <cfset ormflush()> </li></ul><ul><li><cfscript> oEx = EntityNew(“customer”); oEx.setName(“Jane Gum”); oEx.setAge(“35”); oEx.setemail(“nospam@nospam.com”); EntitySave(oEx); ormflush(); </cfscript> </li></ul>
  • 12. <ul><li><cfquery> SELECT </li></ul><ul><li><cfquery name=“qGetCustomers” datasource=“MyDatasource”> SELECT * FROM customer </cfquery> </li></ul><ul><li><cfquery name=“qGetCustomer” datasource=“MyDatasource”> SELECT * FROM customer WHERE name = ‘Jane Gum’ </cfquery> </li></ul><ul><li>ORM </li></ul><ul><li><cfset aCustomers = EntityLoad(“customer”)> </li></ul><ul><li><cfset oCustomer = EntityLoad(“customer”, {name=“Jane Gum”}, true)> </li></ul>
  • 13. <ul><li><cfquery> UPDATE </li></ul><ul><li><cfquery name=“qUpdCustomer” datasource=“MyDatasource”> UPDATE customer SET age = 45 WHERE id = #qGetCustomer.id# </cfquery> </li></ul><ul><li>ORM </li></ul><ul><li><cfset oCustomer.setage(“45”)> <cfset EntitySave(oCustomer)> <cfset ormflush()> </li></ul>
  • 14. <ul><li><cfquery> DELETE </li></ul><ul><li><cfquery name=“qDelCustomer” datasource=“MyDatasource”> DELETE FROM customer WHERE id = #qGetCustomer.id# </cfquery> </li></ul><ul><li>ORM </li></ul><ul><li><cfset EntityDelete(oCustomer)> <cfset ormflush()> </li></ul>
  • 15. <ul><li><cfquery> Output </li></ul><ul><li><cfoutput> #qGetCustomer.name#<br /> #qGetCustomer.age#<br /> #qGetCustomer.email# </cfoutput> </li></ul><ul><li><cfset qGetCustomer.age = 45> </li></ul><ul><li><cfoutput> #qGetCustomer.name#<br /> #qGetCustomer.age#<br /> #qGetCustomer.email# </cfoutput> </li></ul><ul><li><cfquery name=“qUpdCustomer” datasource=“MyDatasource”> UPDATE customer SET age = #qGetCustomer.age# WHERE id = #qGetCustomer.id# </cfquery> </li></ul><ul><li>ORM Output </li></ul><ul><li><cfoutput> #oCustomer.getname()#<br /> #oCustomer.getage()#<br /> #oCustomer.getemail()# </cfoutput> </li></ul><ul><li><cfset oCustomer.setage(“45”)> </li></ul><ul><li><cfoutput> #oCustomer.getname()#<br /> #oCustomer.getage()#<br /> #oCustomer.getemail()# </cfoutput> </li></ul><ul><li><cfset EntitySave(oCustomer)> <cfset ormflush()> </li></ul>
  • 16. <ul><li><cfset aCustomers = EntityLoad(“customer”)> <cfset qCustomers = EntityToQuery(aCustomers)> Loads the customer object array and converts it to a query object. </li></ul><ul><li><cfset aCustomers = EntityLoad(“customer”)> <cfset qEmail = EntityToQuery(aCustomers, “email”)> Loads the customer object array and converts only the ‘email’ column to a query object. </li></ul>
  • 17. <ul><li><cfset aCustomers = ORMExecuteQuery(“from customer”)> </li></ul><ul><li><cfset aCustomer = ORMExecuteQuery(“from customer where id = 27”, true> </li></ul><ul><li><cfset aCustomer = ORMExecuteQuery(“from customer where name=:name”, {name=“Jane Gum”}, true> </li></ul><ul><li><cfset aCustomers = ORMExecuteQuery(“from customer where age=:age and email=:email”, {age=35, email=“nospam@nospam.com”})> </li></ul><ul><li>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. </li></ul>
  • 18. <ul><li>Chapter 8: Coldfusion ORM in the ‘Developing ColdFusion 9 Applications’ reference. </li></ul><ul><li>Adobe’s tutorials (available from the CF9 product page) </li></ul><ul><li>Ben Forta’s, Ray Camden’s, Dan Wilson’s, Ben Nadel’s (and others) blogs are always excellent references for all things ColdFusion </li></ul><ul><li>NVCFUG Adobe Groups Website </li></ul>

×