A Quick Tip on ColdBox Mappings


Do you want to place the ColdBox framework files outside the webroot or create a mapping to use when specifying an association’s cfc path? The following quick tips will show you how to achieve this.

ColdBox Framework Mapping

To place the ColdBox framework folder outside of the webroot, here’s what you need to do:

  • Log into ColdFusion Admin
  • Click on Mappings under Server Settings
  • Enter the following information:
    • Logical Path: coldbox
    • Directory Path: absolute-path-to-coldbox-framework-folder, e.g. D:\coldbox
  • Restart ColdFusion and web server

Please note that if you use IIS web server, then a new coldbox virtual directory is not needed.

ColdBox Application Mapping

In a ColdBox application when specifying the cfc path for orm associations (relationships), or the component’s extend attribute, you need to use the full path from the webroot using dot notation. Here’s an example to illustrate:


component persistent="true" extends="webroot.myApp.model.BaseEntity" table="clients"
{
  property name="client_id" fieldtype="id" generator="identity" setter="false";
  property name="firstname" sqltype="nvarchar" length="100";
  property name="lastname" sqltype="nvarchar" length="100";
  property name="phones" fieldtype="one-to-many" inverse="true" cfc="webroot.myApp.model.phones.Phone" singularname="phone" fkcolumn="client_id";
}

As you can see the name and path to the web application is hard coded. Wouldn’t it be better if we can define the path to the application root folder once and use it many times. Here’s how:

  • Edit Application.cfc at the coldbox application root level
  • Add the following mapping:
    • this.mappings[‘/appRoot‘] = COLDBOX_APP_ROOT_PATH;

With the above mapping, we can now re-write the above model as follows:


component persistent="true" extends="appRoot.model.BaseEntity" table="clients"
{
   property name="client_id" fieldtype="id" generator="identity" setter="false";
   property name="firstname" sqltype="nvarchar" length="100";
   property name="lastname" sqltype="nvarchar" length="100";
   property name="phones" fieldtype="one-to-many" inverse="true" cfc="appRoot.model.phones.Phone" singularname="phone" fkcolumn="client_id";
}

Important notes:

  • If there are exisitng tables, you will need to delete and recreate them by reloading the ORM for the application
  • If the existing tables contains data, you will need to export and generate scripts to re-import them after reloading the ORM if you want to keep them.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s