Skip navigation

Farcry is a great framework because it allows you to extend and re-use components. However, there are many cases where you extended a component (.cfc file) and some of the properties are not required for the scope of the project, but they are inherited from extending the type.

First thing that comes to mind is ‘Why not hide those fields so they don’t appear in the FarCry edit handler interface?’
Well currently there are no easy way to do this, as we can’t hide the field without editing the original component that you are extending from. Also FarCry doesn’t offer a formtool metadata such as ftHidden=”true” or ftDisplay=”none” that you can apply to the properties in your extended component.

So the best way to go about this is, in your extended component, overwrite the ftSeq & ftFieldSet of the properties you want to hide by setting them to null, e.g ftSeq=”” ftFieldSet=””.

By setting those two formtool metadata to null it tells FarCry not to render those field.

Simple and yet effective!

Advertisements

I recently came into a problem where I had to sort the result of a MySQL query by fixed column values.

Example::

SELECT *
FROM table
WHERE x in (a, d, c, b )     

Result::  a b c d

The default ORDER BY clause in this case sorted the result in a descending fashion, however I need the result to be returned as follow.

Result:: a d c b

So how does one get such a result??

By using the the ORDER BY FIELD  clause. This little nifty function allowed me to return the result exactly as hoped and did not require me to further manipulate the results afterwards using another programming language. Below is how you would structure this cool function.

Example::

SELECT *
FROM table
WHERE x in (a, d, c, b )
ORDER BY FIELD( fieldName, a, d, c, b )

Ever wonder why an object data is never updated whenever you update the actual data via a cfquery tag and when you do a getData() from the Farcry frame work, the data doesn’t contain the changes you made previously via cfquery?

Well this is because the data you get from the getData() function are loaded from the Object Broker, which caches the data for fast retrival, therefore when you do an update manually via cfquery tag, this change was not registered in Object Broker. Here’s a handy little snippet of code that will do the trick!

<cfset application.fc.lib.objectbroker.RemoveFromObjectBroker(lObjectIDs=objectid,typename='dmHTML') />

The sample code above allows you to update the cache of a dmHTML object in the Object Broker cache.

Just came across an issue where my ajax response errored after performing a request to retrieve some data returned as a JSON object. The error returned was INVALID JSON. After a couple minutes of debugging I’ve nailed down the data issue. Turns out jQuery parser doesn’t like having single quotes in the response packet, even if the quotes are correctly escaped such as \’. So watch out!

I find it really frustrating sometimes trying to investigate why a schedule task is not executing correctly on a production server because we don’t get access to the CF admin and inspect each schedule task for ourselves. BUT luckily I’ve found this little snippet of code which gonna make our lives much brighter.

<cfset factory = createObject('java', 'coldfusion.server.ServiceFactory')>
<cfset allTasks = factory.CronService.listAll()/>
<cfloop index="i" from="1" to="#ArrayLen(allTasks)#">
    <cfdump var="#allTasks[i]#" />
</cfloop>

This script basically returns a list of schedule tasks on the server!

The title probably wouldn’t make a great deal of sense under normal circumstances… but I am reffering to Coldfusion struct keys and maintaing their case.

In some circumstances it is important to make sure that the case that you enter your struct keys with is not altered, and you are probably thinking that surely whatever case you use it is maintained, but this is only in true under some circumstances. One of my amazing colleagues in our development studio has a situation where the case must be maintained at lower case converting a struct to a JSON packet to be consumed by a facebook application, but because of the way she was building the struct, CF forces the keys to upper case. Check out the example below:

<cfset stPerson = structNew()>
<cfset stPerson.name = "Sandy">
<cfset stPerson.address = "12 Amazing Street">
<cfset stPerson.suburb = "Amazingville">
<cfset stPerson.state = "SA">
<cfset stPerson.phone = "54321234">
<cfdump var="#stPerson#">

and the result:

struct
ADDRESS 12 Amazing Street
NAME Sandy
PHONE 54321234
STATE SA
SUBURB Amazingville

Check out the keys, they are all forced to upper case! Why? , well I found this on another CF developer blog and it sounded pretty good – “ColdFusion internally represents structure keys in uppercase when the keys are created using dot notation. Dot notation is typically how programmers write their code and is considered to be best practice when working with structures.”

So there you go…. but how can we maintain case you ask? well, quite easy, don’t create your structs using dot notation, explicitly set your key names by wrapping them in single quotes, check out this example:

<cfset stPerson = structNew()>
<cfset stPerson['name'] = "Sandy">
<cfset stPerson['address'] = "12 Amazing Street">
<cfset stPerson['suburb'] = "Amazingville">
<cfset stPerson['state'] = "SA">
<cfset stPerson['phone'] = "54321234">
<cfdump var="#stPerson#">

and the result:

struct
address 12 Amazing Street
name Sandy
phone 54321234
state SA
suburb Amazingville

See!! the case of the keys has been maintained!!!

If there is one thing that CF does badly it is the over-zealous creation of whitespace characters that fill your html output with unnecessary garbage that makes it near impossible to make sense of. One thing it is very good at however is making it easy to capture the output of complicated logic without the need to constantly append to a variable by using the cfsavecontent tag. The problem with this is that for every space character inside cfsavecontent it is also added to the variable being used to capture the output so something like this:

<cfsavecontent variable="htmlStr">
	<cfoutput><p>
	<cfloop query="qPets">
		<cfif qpets.cat EQ "dog">
			Some other string about dogs<br>
		<cfelse>
			Some other string about cats<br>
		</cfelse>
	</cfif>
	</cfloop></p></cfoutput>
</cfsavecontent>

Would probably produce something like this:

		<p>
							Some other string about dogs<br />


			Some other string about dogs<br />



					Some other string about cats<br />

	</p>

Luckily we have a tag that helps to repress all this whitespace inside cfsavecontent called cfprocessingdirective with the attribute suppresswhitespace set to true. It has to be used correctly, everything that you want outputted HAS to be wrapped in individual cfoutput tags eg:

<cfsavecontent variable="htmlStr">
	<cfprocessingdirective suppresswhitespace="true"> 
		<cfoutput><p></cfoutput>
		<cfloop query="qPets">
			<cfif qPets.cat eq "dog">
				<cfoutput>Some other string about dogs<br></cfoutput>           
			<cfelse>
				<cfoutput>Some other string about cats<br></cfoutput>           
			</cfif>
		</cfloop>
		<cfoutput></p></cfoutput>
	</cfprocessingdirective> 
</cfsavecontent>

And the results are:

<p>Some other string about dogs<br />
Some other string about dogs<br />
Some other string about cats<br />
</p>

Easy to use, keeps the code looking snazzy and improves your html output.

I was debugging an ajax issue in IE6 today and discovered the getJson() ajax function is not resonding and there were no Javascript error. I did some googling and found the issue in the An IE AJAX gotcha: page caching article. While the article gave us a detailed description of the issue and a solution, I have a simpler suggestion which is to replace the jQuery.getJson() function with jQuery.ajax() instead. Using the ajax() function you can set the dataType to ‘json’ and that will automatically clear the caching for you. Problem fixed!

Just found this cool online tool that allows you to quickly draw up a database schema with export function that output a script for you to import the tables and all of it’s attributes into your selected database type.

http://www.dbschemaeditor.com/OnlineDB.aspx

I don’t need to write anything on the benefits of using CFQUERYPARAM, there have been many articles over the years doing just that. Instead I am going to cut to the chase and simply state –

‘If you are using CFQUERY, then you should always use CFQUERYPARAM when passing in typed data, end of story’.

OK, that was a bit short and dull, here are 3 very good reasons:

1. Reduces the risk of SQL injection attacks
2. Makes data more ‘SQL happy’, escapes problematic characters, eg: single quotes, forward slashes etc…
3. Forces you to think about the data being passed into your query, less mistakes made.

Not enough information for you? read this excellent article from Adobe – http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=queryDB_5.html#1142383