Records in the database need to be updated and new records need to be added using ColdFusion 9's new ORM ( Object Relational Mapping ) features.
ColdFusion 9's ORM ( Object Relational Mapping ) features provide easy access to the powerful features of Hibernate. Beyond simply loading data, ColdFusion provides several functions for adding and updating data, including the entitySave(), entityNew() and ormFlush(). Once again all of this is done without writing a single line of QL!
Note: This Cookbook entry assumes you have read the Cookbook Loading Data with ORM ( http://cookbooks.adobe.com/post_Loading_Data_with_ORM-16431.html ) and thus will skip the enabling of ORM and creating the entity.
Updating Data
The first step to updating data is to load an entity to work with. This is done using the entityLoad() function and specifying the entity, the id of the record to load and setting the unique flag to true. By setting the unique flag to true, it will cause the entity returned to be a single entity object, rather than an array containing a single entity object. If the filter criteria matches more than one record and the unique flag is set, an exception is thrown.
<cfset wine = entityLoad( 'wine', 1, true ) /> <cfdump var = "#wine#" />
The above code will produce the following:
Once the entity is loaded it is possible to update data using the auto-generated methods in the entity itself. ColdFusion creates both getter and setter methods for each property defined in the entity. To update the data the setter of the property to be updated is called and the new value for that property passed in.
<cfset wine = entityLoad( 'wine', 1, true ) /> <cfdump var="#wine#" /> <cfset wine.setwineRating( 99 ) /> <cfdump var = "#wine#" />
The above code produces the following:
That's all you have to do! Once the page is finished loading, ColdFusion will commit all changes to the entity to the database. However, you do have to option of calling the entitySave() function or the ormFlush() function to handle saving. The entitySave() function updates or creates a single record based on the entity passed in and ormFlush() saves all the pending requests and flushes the current ORM session.
Saving Data
Before being able to add new records to the database a small change needs to be made to the entity. The indentity or primary key information must be specified in the entity in order the add a new record. This is done by specifying the generator and fieldtype attribute on the <cfproperty> tag of the primary key, in this case, the wineId property.
<cfproperty name="wineId" fieldtype="id" generator="identity" />
Once those attributes have been specified the entityNew() function is used to create a new entity and the setters for the properties are used to populate the values.
<cfset wine = entityNew( 'wine' ) />
<cfset wine.setwineVintage( 2006 ) />
<cfset wine.setwineRating( 92 ) />
<cfset wine.setwineType( 'Pinot Noir' ) />
<cfdump var="#wine#" />
<cfset entitySave( wine ) />
<cfset wines = entityLoad('wine') />
<cfdump var="#wines#" />
The above code produces the following:
The new record appears at the bottom of the array of entities.
And that's it! ColdFusion 9's ORM features are adding and updating records in the database without writing a single line of SQL code!
+