Example of multiple datasources for ORM in 9.0.1

ColdFusion 9, ORM, ColdFusion 9.0.1

ColdFusion 9.0.1 brings multiple datasources support for ORM. Here is how to set it up.

In this example, we are going to use both the cfartgallery and cfbookclub datasources.  In the Application.cfc we will make the cfartgallery the default datasource.  Optionally we are going to set the dbcreate rules differently for the two datasources.  (We could also do this for the schema, catalog, dialect and sqlscript settings.)

 

component
{
this.name="multi";
this.ormEnabled=true;
this.datasource="cfartgallery";
this.ormsettings = { dbcreate={ cfartgallery="update",
cfbookclub="none"} };
}

 

From cfartgallery we will use the Art table as an object.  As cfartgallery is the default datasource, this is set up as usual:

 

component persistent="true"
{
property name="artID" fieldtype="id" ;
property name="artistID";
property name="artname";
property name="description";
property name="prize";
property name="largeimage";
property name="mediaID";
property name="issold";
property name="samColumn";
}

 

For any tables from the cfbookclub datasource there is an additional attribute "datasource" for the component:

 

component persistent="true" datasource="cfbookclub"
{
property name="bookID" fieldtype="id";
property name="authorID";
property name="title";
property name="bookdescription";
property name="isspotlight";
}

 

We can then get the data and dump it like so:

 

books = entityLoad("BOOKS", {}, {maxResults=2});
writeDump(books);
art = entityLoad("ART", {}, {maxResults=2});
writeDump(art);

 

 Pretty straighforward and pretty cool stuff.

David said:
 
Sam,

What would happen if you had 2 tables with the same name, one from each datasource?
 
posted 45 days ago
Add Comment Reply to: this comment OR this thread
 
samfarmer said:
 
As each entity needs to have its own unique name you would use the table attribute of the component on the second entity.

Lets assume we have two tables called Books. For the first datasource define it as above:
component persistent="true" datasource="cfbookclub"

For the second datasource (amazon) add a BooksOther.cfc and define the component as:
component persistent="true" datasource="amazon" table="Books"

The table attribute defaults to the name of the component.
 
posted 45 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Twitter
You should follow me on Twitter here
About Me
I am a 34-year old Web Developer specializing in ColdFusion. I live and work in downtown Washington, DC with my wife and two daughters. Read more About Me

2007 CFeMmy Best Newcommer winner
As voted on by fellow CF Bloggers.