ORM and Multiple Environments

I've been trying to use ORM for my service layer in my project. And I've got 3 environments - dev, staging and production. The Problem with using ORM is that Wirebox Injection does not kick in when ORM settings are setup (from Application.cfc)

My Application.cfc has got this currently.

this.ormsettings = {
      cfclocation = "model",
      dialect = "MySQLwithInnoDB",
      dbcreate = "none",
      eventHandling = true,
      logSQl = true,
      autoManageSession = false,
      flushAtRequestEnd = false,
      eventHandler = "model.ORMEventHandler"
    };

So all the ORM based stuff depends on this.datasource in Application.cfc, whereas I've got different datasources for each environment in my ColdBox.cfc.

Is there a solution to this problem ?

Please advise.

Anuj Gakhar

Why do you have different environments for your database? If you are using MySQL in production (or insert any dbms here) then you should be developing and testing against that. I am not sure why you need separate environments for that, can you elaborate?

I have some similar setups.

I detect my environment in onApplicationStart() within Application.cfc. I set my environment into an application variable and in the ColdBox.cfc config I use the detectEnvironment() function to inform ColdBox what env we are in.

public string function detectEnvironment()
{
switch(application.environment)
{
case ‘workstation’: return ‘office’; break;

case ‘development’: return ‘development’; break;

case ‘stage’: return ‘stage’; break;
default:
return ‘production’;
break;
}
}

Also, I use the machine name(s) to detect environment using:
application.machine = createObject(‘java’,‘java.net.InetAddress’).getLocalHost().getHostName()

Especially on dev server(s) I have found this avoids errors when some IP scanning bot comes by and passes an invalid hostname.

-A

Dan,

It’s the datasource name that differs based on which environment I am in

On development (locally) - I have datasource = “mydsn” - pointing to a local Mysql Server
On staging (on the actual server) - the datasource = “mydsn_staging” - pointing to a different database in the MySQl Server
And on Production - datasource = “mydsn_live” - pointing to the LIVE Mysql database

Aaron,

That sounds great. So I assume you change the “datasource” in this.ORMSettings in Application.cfc in onApplicationStart() as well?

Yes.

ColdFusion itself is a great framework. I try to use it.

-A

Thanks Aaron :slight_smile:

I’ve got another scenario with this ORM stuff. I have 2 different databases that my application works with. The separation is mainly based around users. So, the user accounts sit in a separate database and the rest of the stuff sits in a separate database. And the same DB user has access to both.

In my application.cfc, this.datasource = “main_dsn” - which points to the other DB, not the users DB. Now, in a normal CF query, I can simply do queries like below, if I was to access the User info :-

select r.id, r.rating from tblratings r
inner join users_development.tbluser u on u.id = r.userid

this cross-database query can access the data from Users DB just fine.

However, in ORM, I have managed to achieve this by using the “catalog” attribute on the persistent CFC.

So, my User.cfc looks like this

component persistent=“true” output=“false” entityname=“User” table=“tbluser” catalog=“users_development”
{
property name=“id” fieldtype=“id” generator=“identity”;
property name=“username” ormtype=“string”;
property name=“firstname” ormtype=“string”;
property name=“lastname” ormtype=“string”;
property name=“email” ormtype=“string”;

public User function init() output=“false”
{
return this;
}
}

Now, this works OK if there was only one environment. How can I manage multiple environments in this setup? I can change this.datasource in Application.cfc based on which environment I am in but how do I change the “catalog” attribute based on which environment I am in ? The problem only happens when I have multiple environments on the same server, otherwise it’s OK.