Updating Datasource with ColdFusion Bean
Posted by Stephen Weyrick | Tags: coldfusion , flex
John Gagliardi(cftips.net) and I have been working on a new flex application that will be used as a project tracker for work related purposes. John is building the Flex or UI side of the application, while I am building the database and ColdFusion backend. We are trying to build the entire application using OOP design priciples; specifically MVC. After building the database and basically getting a great start on the project, I am starting to realize how powerful OOP can actually be. Today we created a task updater that is controlled by horizontal date slider, and I wanted to share some of my backend code which will hopefully show just how easy it is to update a simple CRUD app.
In this function, I am accepting the arguments for a taskID, startDate, and endDate. I then create a gatway object which is used to grab the task by the taskID; it is then passed into my function to convert the returning query into a structure. Next I pass this struct to the bean and call my setters to update the specific startDate and endDate arguments. Lastly, I call my update method of my previously set DAO object, and like magic it works. I only wrote six or seven lines of code; letting the generator do the rest. A gateway, DAO, and bean can easily be created using the Illudium PU-36 Code Generator, which we are using in this app to generate most of our code. We hope to have a working example of the project tracker up soon, so if you have a chance, check back here or at cftips.net.
<cffunction name="updateTasks" access="public" returntype="void"
hint="used to update a specific task with time sliders">
<cfargument name="taskID" type="string"required="yes">
<cfargument name="startDate" type="string" required="yes">
<cfargument name="endDate" type="string" required="yes">
<cfset var objTask = createObject( "component",
"projectTracker.components.cfc.model.tasksGateway" )
.init( dsn= "dsnDB7" ) />
<cfset var objTaskDAO = createObject( "component",
projectTracker.components.cfc.model.tasksDAO" ).init( dsn = "dsnDB7" ) />
<cfset var task = objTask.getByAttributesQuery( taskID = #arguments.taskID# ) />
<cfset var updateTask = ""/>
<cfset var taskToStruct = objTask.queryRowToStruct( qry = "#task#" )/>
<cfset objTaskBean =
createObject( "component",
"projectTracker.components.cfc.model.tasks" ).
init(argumentCollection = "#taskToStruct#" ) />
<cfset objTaskBean.setstartDate( #arguments.startDate# ) />
<cfset objTaskBean.setendDate( #arguments.endDate# ) />
<cfset updateTask = objTaskDAO.update( objTaskBean ) />
</cffunction>
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment