Question

I faced an issue while trying to use SQL function in where clause of CfWheels's findAll() method.

<cfset currencyInfo= model('pricelist').findAll(select="currencyid,name",where="YEAR(startDate)=#params.year#")>

The confusing thing here is that while using the sql function in the select clause of the same statement, it works like a charm.

<cfset currencyInfoTest= model('pricelist').findAll(select="currencyid,name,YEAR(startDate) AS Year",where="id=5")>

I guess CFWheels might be processing the column mappings in the select clause only and not any calculated or operated syntax in the where clause.

I need to use some sql functions in the where clause as above. Any ideas?

Was it helpful?

Solution

You're right in your assumption that CFWheels does not allow SQL functions in the where argument. It parses the string for property names so it can map them to columns in the database, and it also applies <cfqueryparam> bindings to the values passed in. But it is unable to parse out calls to SQL functions.

However, you can create a calculated property based on the SQL function that you need and query against that.

In models/PriceList.cfc:

function init() {
  property(name="startDateYear", sql="YEAR(pricelists.startdate)";
}

In your findAll call:

currencyInfo = model('pricelist').findAll(select="currencyid,name", where="startDateYear=#params.year#");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top