Saturday, 10 January 2015

CF - Application.cfc/Application.cfm lookup order

Before writing this post, I was thinking that when coldfusion server get the request for page, It will search Application.cfm/Application.cfc in current directory until webroot by default and that is final.

But I was wrong.

By default coldfusion will search until system directory i.e. if we installed coldfusion on C:/ it will serach until C:/.

We can change this setting though coldfusion administrator.We can set the order in which ColdFusion searches for Application.cfm or Application.cfc if it is not found in the current project folder. We can set ColdFusion to search as follows:
§  default search order: ColdFusion looks for an Application.cfc/Application.cfm file from the current folder until the system root directory. On Windows, this could be C:\ and on UNIX, /opt.
§  till web root: ColdFusion looks for an Application.cfc/Application.cfm file from the current folder till web root.
§  in web root: ColdFusion looks for an Application.cfc/Application.cfm file in the current folder or web root.

We can change this settings  though coldfusion administrator.
Go to Server Settings à Server in CF admin.



Reference :
http://help.adobe.com/en_US/ColdFusion/9.0/Admin/WSc3ff6d0ea77859461172e0811cbf3638e6-7ffc.html

Sunday, 4 January 2015

CF9: ORM (Object Relational Mapping)

Relational databases are the core of most enterprise applications. However, when you map a relational database to objects, it becomes a challenge. Object relational mapping (ORM) is a programming framework that allows you to define a mapping between application object model and the relational database.
In an object model, the application objects are not aware of the database structure. Objects have properties and references to other objects. Databases consist of tables with columns that maybe related to other tables. ORM provides a bridge between the relational database and the object model.
By using ORM, you can access and update data entirely using the object model of an application. ORM provides features such as:
§  Database vendor independence
§  Caching
§  Concurrency
§  Performance optimization
Before ORM release in coldfusion 9, database access was achieved by:
§  Managing relational data using tags such as cfquery, cfinsert, and cfupdate, which handle SQL statements.
§  Managing objects using ColdFusion components (CFCs), and object lifecycle using the application itself
§  Writing SQL queries for each CFC, even for basic CRUD (Create, Retrieve, Update, and Delete) operations.
The complexity of managing these tasks increases as your application grows.
ColdFusion ORM automates most of these tasks, which:
§  Makes application code cleaner and more manageable
§  Enhances your productivity and lets you develop database applications faster
§  Creates applications that can run faster because of built-in ORM optimizations
§  Minimizes the amount of code you write
Apart from providing a framework for mapping the object model with the relational database, ColdFusionORM provides data query and retrieval facilities.
Go through the following code files which will give idea about how CRUD (Create, Retrieve,Update,Delete) actions can be performed using ORM -
Application.cfm

<cfcomponent displayname="Application" output="true">
<cfset This.name = "ORMApplication">
<cfset This.Sessionmanagement=true>
<cfset This.Sessiontimeout="#createtimespan(0,1,0,0)#">
<cfset This.applicationtimeout="#createtimespan(5,0,0,0)#">
<cfset this.ormenabled = "true">
</cfcomponent>

Artists.cfm
<cfcomponent persistent="true">
    <cfproperty name="id" column = "ARTISTID" generator="increment">
    <cfproperty name="FIRSTNAME">
    <cfproperty name="LASTNAME">
    <cfproperty name="ADDRESS">
    <cfproperty name="CITY">
    <cfproperty name="STATE">
    <cfproperty name="POSTALCODE">
    <cfproperty name="EMAIL">
    <cfproperty name="PHONE">
    <cfproperty name="FAX">
    <cfproperty name="thepassword">
</cfcomponent>

     
Index.cfm

<!--- <cfdbinfo
          datasource="cfartgallery"
          name="res"
          type="columns"
            table="ARTISTS">

<cfdump var="#res#"> --->

<!--- CRUD OPERATIONS -  By Using ORM--->

<!--- 2. Retrieve --->
<cfset result = EntityLoad("ARTISTS")>

<!--- 1. Create --->
<cfscript>
try {
    newArtistObj = EntityNew("artists");
   
    newArtistObj.setfirstname("dattatray");
    newArtistObj.setlastname("Smith");
    newArtistObj.setaddress("5 Newport lane");
    newArtistObj.setcity("San Francisco");
    newArtistObj.setstate("CA");
    newArtistObj.setPostalCode("90012");
    newArtistObj.setphone("612-832-2343");
    newArtistObj.setfax("612-832-2344");
    newArtistObj.setemail("jsmith@company.com");
    newArtistObj.setThePassword("jsmith");
   
    EntitySave(newartistobj);
    ormflush();
} catch(Exception ex) {
    WriteOutput("<p>#ex.message#</p>");
}
</cfscript>

<!--- 3. Update --->
<cfset newArtistObj.setphone("612-832-1311")>
<cfset ormflush()>

<!--- 4. Delete --->
<cfset EntityDelete(newArtistObj)>

<cfset ormflush()>