Default TimeStamp Gotcha with ColdFusion ORM

Let’s say for example you have a simple forum on your site. When ever a new post is added, you want to associate a date/time with the post.

Using SQL and ColdFusion there are three ways to do this:

INSERT INTO
    Forum(body, dateTimeInserted)
VALUES(
    <cfqueryparam value="#form.body#" cfsqltype="cf_sql_longvarchar">,
    <cfqueryparam value="#now()#" cfsqltype="date">
)

OR

INSERT INTO
    Forum(body, dateTimeInserted)
VALUES(
    <cfqueryparam value="#form.body#" cfsqltype="cf_sql_longvarchar">,
    GETDATE()
)

OR

Set your default column value to ‘GETDATE()’ (in MSSQL)

INSERT INTO
    Forum(body)
VALUES(
    <cfqueryparam value="#form.body#" cfsqltype="cf_sql_longvarchar">
)

The third has always been the most recommended method. The reason being less network traffic and the SQL Server is the common denominator between the SQL Server and your application servers.

Now introduce ColdFusion ORM into the mix. The default value method still works when you set the property attribute of “insert” to false in your bean.

component persistent="true" table="Forum" {
    property name="ID" fieldType="id" generator="native";
    property name="body";
    property name="dateTimeInserted" insert="false";
}

However the case where it doesn’t work as needed is when you save the entity and load all the entities within the same ORM Session (ColdFusion request).

So let’s say our controller method looks like this in FW/1:

void function forum( required struct RC ) {
    if( structKeyExists( RC, "body" ) ) {
        getForumService().add( body = RC.body );
    }
    RC.forumEntities = getForumService().get();
}

Because the entity we just saved is still in the same ORM session, it doesn’t look at the database again for it. But because we rely upon SQL to add the timestamp, ORM doesn’t know about it yet. Thus it returns an empty string instead of the date and time it was added.

So we get something like this record set:

1|’Body 1’|’1/25/2014′
2|’Body 2’|’1/26/2014′
3|’Body 3’|”

This is of no use to me. I suppose I could loop through the array and if one has an empty date/time added value, then reload that entity, but that seems like overkill for my application.

So in the end, unless precise date/time stamps are needed, I’m going to use the application server’s date/time (now()) instead of SQL’s GETDATE() default.

void function add( required string body, required date dateTimeInserted ) {
    var forumEntity = entityNew('forum');
    forumEntity.setBody(arguments.body);
    forumEntity.setDateTimeInserted(arguments.dateTimeInserted);
    entitySave(forumEntity);
    ormFlush();
};

#coldfusion-2, #orm

My New Experience Getting ColdFusion Builder 2 Console Working

I have ColdFusion 9 Developer Edition installed on my Windows 7 machine running IIS7. Normally my ColdFusion 9 Application Server service starts automatically for me.

Recently I have started learning and developing with ORM. Because of this, I need to figure out what ORM is asking the SQL server to make sure it’s not doing anything crazy.

To do this I turned on logsql in ormSettings config in application.cfc. Then found that it would log to a file or the Console view.

But alas, nothing showed in my Console view, and yep, I did have a server configured in the Servers view.

So I find out that I need to stop/start the server in the servers view to get this working. After trying that it pretty much lied to me and said it did when it really didn’t.

At that point I stopped the service in the Windows service and tried to start from ColdFusion Builder. No such luck.

Then I tried restarting ColdFusion Builder w/ Administrator rights. BAM – now I can get the CF service started.

But I notice something interesting. The service in the Windows Service Manager still shows stopped after a refresh. But alas my Console works perfectly.

So I have no idea what is going on, but this is what I figure needs to happen to get this all working correctly:

  1. Change the ColdFusion 9 Application Server properties of startup type to manual and stop the service.
  2. Close ColdFusion Builder if already open and start it as Administrator (right click icon and select Run As Administrator)
  3. In the Servers View in ColdFusion Builder, edit your server config and auto start and stop the CF Server. (optional but recommended… If you close CF Builder, you will need to restart the server anyway)
  4. Select your server and click the green run arrow.
  5. You should see ColdFusion startup with a bunch of output in the Console at this point.
At this point I would suggest modifying your shortcut to always run as administrator. This has gotten me many times already, so I finally made this change.
  1. Right click shortcut and select properties
  2. Click Advanced button
  3. Turn on “Run as administrator”
  4. OK > OK

What a mess! Needs to be a slicker way of getting this rolling without an hour of research and messing with Administrator rights. But hope this helps my memory and anyone else scratching their head!

#orm