coldfusioncfwheels

cfwheels Find Records and Output as JSON Encoded


I am new to ColdFusion, currently using CFWheels Framework. I have a snippet that send a ajax request from the view using jquery post with two parameters name and time.

$(".building").on("blur", function() {
      $.post("index.cfm/audit/building", { name: "John", time: "2pm" })
          .done(function( data ) {
            alert( "Data Loaded: " + data );
          });
        });

My controller action

<cffunction name="building">
        <cfscript>
            categories = model("buildings").findByKey(params.name);

            test = params.name & params.time;
            renderText(test);
        </cfscript>

    </cffunction>

My Model

<cfcomponent extends="Model">
    <cffunction name="init">
        <cfset table("buildings")>
        <cfset hasMany("rooms")>
    </cffunction>
</cfcomponent>

I wish to do a simple task as following

  1. check if any building by passed name exist in database
  2. if so then return the row in json format, like echo json_encode in php
  3. else create new record and redirect to another action with params

I am stuck on step 1, and its telling me

The value for cfqueryparam cannot be determined

What does this mean? Please help, also if anyone can also tell me how to render query data in json form for jquery post to read.


Solution

  • just sharing the answer.

    <cffunction name="building">
            <cfscript>
                buidling = model("buildings").findOneByName(params.name);
    
                if(IsObject(buidling)) {
    
                    //return json format
                    renderText(SerializeJSON(buidling));                                
    
                } else {
    
                    //enter new record
                    new_building = model("buildings").new();
                    new_building.name = params.name;
                    new_building.save();
    
                    //return id json
                    renderText(SerializeJSON(new_building));            
                }   
            </cfscript>
    
        </cffunction>