Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, it depends. How often will this query data be updated? If it really is unchanging, then onApplicationStart() is a fine place to put it. However, if it will change every so often, you can just tell Coldfusion to <a href="http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_17.html" rel="nofollow noreferrer">cache the query</a> for <a href="http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_c-d_23.html#1102715" rel="nofollow noreferrer">a certain period of time</a>, then you don't need to mess with onApplicationStart(), but rather when you call the query it will return the cached result automatically (within your specified time period).</p> <p>Regardless, I would write a custom function to retrieve the data. Then it will be trivial to call it from onApplicationStart() or elsewhere.</p> <p>Startup.cfc: (Named whatever you like)</p> <pre><code>&lt;!--- Replace the datasource name with your db name ---&gt; &lt;cffunction name="getStartupQuery" hint="Returns a query recordset for startup"&gt; &lt;cfargument name="datasource" required="no" type="string" default="OtherAppDB"&gt; &lt;!--- Init the query variable ---&gt; &lt;cfset var result = queryNew("id")&gt; &lt;!-- Get the query dataset ---&gt; &lt;cfquery name="result" datasource="#arguments.datasource#"&gt; YOUR QUERY HERE &lt;/cfquery&gt; &lt;cfreturn result&gt; &lt;/cffunction&gt; </code></pre> <p>Application.cfc: (Just the important parts)</p> <pre><code>&lt;cffunction name="onApplicationStart"&gt; &lt;!--- init the startup.cfc, then retrieve the data and save it to the application scope. Remember the component name must match your component above ---&gt; &lt;cfset var startup = createObject("component", "startup")&gt; &lt;cfset application.varFromOtherDB = startup.getStartupQuery()&gt; &lt;cfreturn true&gt; &lt;/cffunction&gt; </code></pre> <p>Now, you should be able to access this variable from any CFM or CFC in your application using:</p> <pre><code>&lt;cfset myNewVar = application.varFromOtherDB&gt; or #application.varFromOtherDB# </code></pre> <p>IF you use the onApplicationStart() method, I highly recommend implementing a method to reinit the application. For an example, <a href="https://stackoverflow.com/questions/1902109/how-do-i-reset-application-cfc-without-resetting-the-server-instance">see this other discussion</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload