How does Coldfusion's createObject() function search for an component?
Asked Answered
T

3

5

I'm having some problems understanding the createObject() function, which the documentation says is used like CreateObject("component", component-name).

In the documentation, it is mentioned that Coldfusion searches for the component in "Directories specified on the Custom Tag Paths page of ColdFusion Administrator"

But it is not working in my case. I have a folder mapped in CF admin for custom tags, inside that folder I am placing a folder named "mycfcs" where my cfc is present named Item.cfc

In the test page, I am creating the object in this way:

<cfset testObj = createobject("component","mycfcs.Item")>

But it is throwing an error "Could not find the ColdFusion component or interface".

Tisiphone answered 14/12, 2013 at 13:49 Comment(3)
@Sunny: "custom tag mapping name" means which name?Tisiphone
you have created custom tag path for components you have create mapping in server settings -> mappingsPoche
@Sunny: But in the docs it is mention that Directories specified on the Custom Tag Paths page of ColdFusion Administrator not in mappings? or am i wrong? Link: help.adobe.com/en_US/ColdFusion/10.0/Developing/…Tisiphone
C
5

Create a per application mapping pointing to the folder with your CFCs in Application.cfc

this.mappings["/cfc"] = {path to your CFCs};

Then in your createObject() call, use the dot delimited path to your CFC.

createObject("component", "cfc.Item");

If you have sub-folders, you would access it as such

createObject("component", "cfc.subFolder.Item");
Chiquita answered 15/12, 2013 at 0:59 Comment(2)
In ColdFusion 10 you could also just do obj = new cfc.subFolder.Item()Brina
@MattBusche I think it was introduced back in CF8.Obaza
A
5

According to this Adobe link:

When you instantiate or invoke a component, you can specify the component name only, or you can specify a qualified path. To specify a qualified path, separate the directory names with periods, not backslashes. For example, myApp.cfcs.myComponent specifies the component defined in myApp\cfcs\myComponent.cfc. For additional information, see Saving and naming ColdFusion components.

ColdFusion uses the following rules to find the specified CFC: ◾ If you use a cfinvoke or cfobject tag, or the CreateObject function, to access the CFC from a CFML page, ColdFusion searches directories in the following order:

  1. Local directory of the calling CFML page
  2. Web root
  3. Directories specified on the Custom Tag Paths page of ColdFusion Administrator

Make sure you have the correct name, your component filename ends in CFC (NOT CFM), the path reference in your createObject command is correct, and your case is correct (depending on OS).

Here is some code I use to load CFCs dynamically:

<cffunction name="getNewObject" hint="Gets a new object with the specified type, module, project and settings" access="private">
  <cfargument name="myDocObj" required="yes" hint="Document Object to create a report from">     
  <cfscript>
      //Create path based on arguments
      var objectPath = createPath(arguments.myDocObj);
        var tmpObj = createObject("component", "#objectPath#").init(this.Settings)

      // return new object based on objectPath, which uses module and type to derive the name of the cfc to load
      return tmpObj;
  </cfscript>
</cffunction>

<cffunction name="createPath" access="private">
  <cfargument name="myDocObj" required="yes">
  <cfscript>
    var module = LCase(arguments.myDocObj.get('module'));
    var type = LCase(arguments.myDocObj.get('documentType'));
    // return the name of the cfc to load based on the module and type
    return "plugins.#module#_#type#";
  </cfscript>
</cffunction>
Allenaallenby answered 22/4, 2014 at 12:27 Comment(0)
P
-2

Just change mycfcs.Item to Item.

On our development server, we have "D:\DW\CF_stuff\CustomTags" specified as the custom tag location. I have a file located at, "I:\CF_stuff\CustomTags\Components\CompareData\DW-ScheduleBookdan.cfc". If I run this code:

abc = CreateObject("component", "DW-ScheduleBookdan");
WriteDump(abc);

I see the object's properties and methods.

What are you doing differently?

Pishogue answered 14/12, 2013 at 14:57 Comment(7)
In what way is it not working? Are you getting the same error message?Pishogue
Yes it is showing the same error.One question do I need to create the mapping in server settings -> mappings or in custom tags path?Tisiphone
just try creating in server settings -> mappingsPoche
In mappings it is working but in docs help.adobe.com/en_US/ColdFusion/10.0/Developing/… it is mentioned custom tags path. Any thoughts?Tisiphone
@DanBracuk: you are correct, but my case is bit different, in the mapped directory there is a sub directory named checkout so i am using this syntax <cfset testObj = createobject("component","checkout.Item")> It is not working but when i am movining the Item.cfc to the directly mapped folder it is working. Please help..Tisiphone
So in other words, you are not following my suggestion. You don't need to reference the subdirectories in your createobject command.Pishogue
<cfset testObj = createobject("component","Item")> this is also not working for my case..Tisiphone

© 2022 - 2024 — McMap. All rights reserved.