Re: How to Handle Objects when Doing Several Updates and Once?

2009-09-29 Thread Kevan Stannard

Hi Glyn
From your example I am guessing there is some thing that these options are
associated with. Let's assume it's a product with various options. Within
CFML I would probably have code something like:

// create the object
cfset productBean = createProductBean()

// populate with the primary fields (eg optionId might be here?)
cfset populate(productBean)

// Set the individual options
cfloop from=1 to=#form.qty# index=i
cfset productBean.setOption(form[value  i])
/cfloop

// Save the product
cfset saveProduct(productBean)

The options /are/ stored as some kind of collection (array, struct, etc)
inside the product bean and you would loop over that when saving.
saveProduct() could call productBean.getOptions() to get the collection of
options.

I have left out quite a lot but hopefully this gets the idea across.

Kevan


2009/9/28 Glyn Jackson glyn.jack...@newebia.co.uk


 I am trying to work with objects and better understand how to use them. So
 far it has been straight forward when updating/creating/deleting one record
 at a time. But I now need to update many records at once is getting a bit
 tricky.

 I simplified version of what I am doing so far is below...


 I create an object...
 var productBean = variables.storeService.createProductBean(); //Create
 Product Bean

 Push the data into it using this very useful plugin in CB
 getPlugin('beanFactory').populateBean(productBean);//The Magic Bean Machine

 and then simply update my DAO via my service like so
 variables.storeService.saveProduct(productBean)

 my question
 However I am now faced with a situation where I need update several records
 at the same time. So my question is how to I handle this with an object? do
 I have to repopulate my object in a loop and then do the database updates
 one at a time? I can see this causing many issues what if the write fails
 halfway etc, and it means writing some sort of array to store all the data
 while its being looped over, sounds cumbersome!

 Below is how I would have done the update before objects in a procedural
 manner if you get what I mean...

 cfloop from=1 to=#FORM.qty# index=i
cfscript
value = ;
if (i IS 1) {
value = #FORM.value1#;
}
else if (i IS 2) {
value = #FORM.value2#;
}
else if (i IS 3) {
value = #FORM.value3#;
}
else if (i IS 4) {
value = #FORM.value4#;
}
else if (i IS 5) {
value = #FORM.value5#;
}
else if (i IS 6) {
value = #FORM.value6#;
}
else if (i IS 7) {
value = #FORM.value7#;
}
/cfscript

 cfif NOT value IS 
 cfquery name=addValue datasource=dbSource
 INSERT INTO optionValues (value, optionId) VALUES ('#value#', '#optionId#')
 /cfquery
 /cfif
 cfset i = i + 1
 /cfloop
 cfset success = TRUE


 how do other people do this sort of update using objects, any example, blog
 posts, ect would be very useful. Thanks.

 :)


 

~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:326737
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4


Re: How to Handle Objects when Doing Several Updates and Once?

2009-09-29 Thread Glyn Jackson

Thanks you, yes thats the way I was thinking, makes sense. 

~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:326761
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4


How to Handle Objects when Doing Several Updates and Once?

2009-09-28 Thread Glyn Jackson

I am trying to work with objects and better understand how to use them. So far 
it has been straight forward when updating/creating/deleting one record at a 
time. But I now need to update many records at once is getting a bit tricky.

I simplified version of what I am doing so far is below...


I create an object...
var productBean = variables.storeService.createProductBean(); //Create Product 
Bean

Push the data into it using this very useful plugin in CB
getPlugin('beanFactory').populateBean(productBean);//The Magic Bean Machine

and then simply update my DAO via my service like so
variables.storeService.saveProduct(productBean)

my question
However I am now faced with a situation where I need update several records at 
the same time. So my question is how to I handle this with an object? do I have 
to repopulate my object in a loop and then do the database updates one at a 
time? I can see this causing many issues what if the write fails halfway etc, 
and it means writing some sort of array to store all the data while its being 
looped over, sounds cumbersome!

Below is how I would have done the update before objects in a procedural manner 
if you get what I mean...

cfloop from=1 to=#FORM.qty# index=i
cfscript
value = ;
if (i IS 1) {
value = #FORM.value1#;
}
else if (i IS 2) {
value = #FORM.value2#;
}
else if (i IS 3) {
value = #FORM.value3#;
}
else if (i IS 4) {
value = #FORM.value4#;
}
else if (i IS 5) {
value = #FORM.value5#;
}
else if (i IS 6) {
value = #FORM.value6#;
}
else if (i IS 7) {
value = #FORM.value7#;
}
/cfscript

cfif NOT value IS 
cfquery name=addValue datasource=dbSource
INSERT INTO optionValues (value, optionId) VALUES ('#value#', '#optionId#')
/cfquery
/cfif
cfset i = i + 1
/cfloop
cfset success = TRUE  


how do other people do this sort of update using objects, any example, blog 
posts, ect would be very useful. Thanks.

:)


~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:326689
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4