Learning ColdBox: Multiple Updates uisng a Object

My venture in ColdBox has been so far very enjoyable. It has really changed the way I plan my applications. My stumble in to using objects (correctly) has also brought with it some challenges and changed my approach to building maintainable and reusable applications.

So far my blog entries on ColdBox have mainly been on objects, validation and persistent, all straight forward when doing simple updating/creating/deleting actions.

Anyway, going back to challenges and objects, my first real stumbling block came a few weeks ago. How do I insert multiple records from data that comes from a form using a object?

I asked this question on both the ColdBox forums and CFTALK list. They all make it sound so simple, not sure they had this in mind.

My problem was as follows: when creating/updating a product I need to insert/update or delete multiple records for the product options based on what the user ticked or not ticked. So my first attempt worked that's a start right? here goes....

First I populated all my options for the products in my view. Below is the option view created in my option tab.

   view plainprintabout
 <cfset runEvent(event='store.getOption',private=true)>
 <cfoutput>
  <tr>
  <td class="borderleft"> 
  <input type="checkbox" <cfif current.idexists eq 1> checked="checked" </cfif> name="optionToUpdate" value="#rc.getOption.valueId#" /></td>
  <td>    #rc.option.value#</td>
  <td> £
  <input value="#NumberFormat(current.optionPrice,99.99)#" type="text" name="optionPrice_#rc.getOption.valueId#" size="3" />
  extra</td>
10   <td> </td>
11   <td> </td>
12   </tr>
13  </cfoutput>

This form submits to my 'save' handler. So how do I handle all the product options? My options are recorded in a linking table due to my normalization, this sort of relationship allows multiple options to be assigned to a product. However this makes it very difficult for me because I will have to delete the option from the linking table or add it based on whether it's been ticked.

Save Handler

   view plainprintabout
 <!---Save Product--->
 <cffunction name="productSave" access="public" returntype="void" output="false">
 <cfargument name="Event" type="coldbox.system.beans.requestContext">
  <cfscript>
  var rc = event.getCollection();// RC Reference
 
var productBean = variables.storeService.createProductBean(); //Create Product Bean Ready For Population
 
    var errors = ';
 
 
10  getPlugin('beanFactory').populateBean(productBean);// Populate Bean From RC Scope
11  
errors = productBean.validate(rc);//Check For Validation Errors
12  

13  if NOTT ArrayLen(errors)){//No Validation Errors For Product
14  

15   if ( variables.storeService.saveProduct(productBean)) {//Update Was OK
16  

17  
18   if isdefinedd('rc.optiontoupdate')){//if we have any options update them
19  
             runEvent(event='store.updateOptions',private=true); // update all options
20  
             }
21  
22  
23          
24       }
25  
26  
</cfscript>

Above is my 'save' handler , after the product object is populated and validated it creates/updates the product. If this was successful a private function 'updateOptions' is run. This is where I will 'try' and update my options.

The next code is not good, in fact it could be called a performance issue, but it works and well.

   view plainprintabout
 <!---Update Product Options--->
 <cffunction name="updateOptions" access="private" returntype="any" output="false" hint="">
  <cfargument name="Event" type="coldbox.system.beans.requestContext">
  <cfscript>
     var rc = event.getCollection();// RC Reference
 
    var optionBean = variables.storeService.createProductOptionsBean(); //Create Bean Ready For Population
 

         for(i=1;i lte listlen(#rc.optionToUpdate#,",");i++){     
                 tempVal = #listgetat(rc.optionToUpdate,i,",")#;    
10                  rc.valueID = #tempVal#; //Option ID
11  
                rc.optionPrice = rc["optionPrice_" & #tempVal#]; //Option Price
12  
                getPlugin('beanFactory').populateBean(optionBean); //Populate Option Bean
13  
                variables.storeService.saveproductoptions(optionBean);//Save Option
14  
         }
15      
</cfscript>
16  </cffunction>

I loop over each ticked option populate the option bean and update the database.

   view plainprintabout
 <!--- Save Product Options --->
 <cffunction name="saveproductoptions" access="public" output="false" returntype="boolean">
  <cfargument name="bean" type="admin.model.objects.productoptions" required="true" />
  <cfset result = variables.optionGateway.saveproductoptions(arguments.bean) />
  <cfreturn result />
 </cffunction>

In the DAO layer if the option exists its created if not its deleted.

   view plainprintabout
 <!---Save productoptions--->
 <cffunction name="saveproductoptions" access="public" output="false" returntype="boolean">
  <cfargument name="productoptions" type="admin.model.objects.productoptions" required="true" />
  <cfset var success = false />
  <cfif existsproductoptions(arguments.productoptions)>
  <cfset success = updateproductoptions(arguments.productoptions) />
  <cfelse>
  <cfset success = deleteoptions(arguments.productoptions) />
  </cfif>
10  
11   <cfreturn success />
12  </cffunction>

This is my first attempt, I am thinking now maybe creating an array of objects and submit them all to the service layer in one go.

Related Blog Entries

TweetBacks
Comments
 

About Me

Glyn Jackson, 27 years old, MD and senior developer of a development firm based in Staffordshire called Newebia Ltd. Academic background in BSc Information System & Internet Commerce. Online marketing expert (EE Ranked) and .NET developer. Has been developing with ColdFusion for 5 years and loves it. "I am not a veteran in ColdFusion but I do work on challenging projects which help me learn more about ColdFusion and if I can contribute to the community in anyway then, it's all good!"

Recommends

  • ColdFusion