Another way to do it is to serialize the object in a base64 string.
I've had some great success using this method to persist complex
objects to the database.
<cfscript>
function serializeObject(object){
var local = structNew();
local.byteOut = createObject("java", "java.io.ByteArrayOutputStream");
local.byteOut.init();
local.objOut = createObject("java", "java.io.ObjectOutputStream");
local.objOut.init(local.byteOut);
local.objOut.writeObject(arguments.object);
local.objOut.close();
return toBase64(local.byteOut.toByteArray());
}
function deserializeObject(objectString){
var local = structNew();
local.inputStream = createObject("java",
"java.io.ByteArrayInputStream");
local.objIn = createObject("java", "java.io.ObjectInputStream");
local.returnObj = "";
local.inputStream.init(toBinary(arguments.objectString));
local.objIn.init(local.inputStream);
local.returnObj = local.objIn.readObject();
local.objIn.close();
return local.returnObj;
}
</cfscript>
I've been using this in http://sessionswap.riaforge.org/ for a long time.
John Blayter
Land line: 303.731.3009
Mobile: 303.325.1979
http://www.blayter.com/john/
Denver ColdFusion User Group Manager
http://denvercfug.org/
On Wed, Apr 6, 2011 at 12:02 PM, Jeff Garza <[email protected]> wrote:
>
> Arrays are easy as you can simply use Arraytolist() to convert it to a
> delimited text string and then use ListtoArray() to convert back once you
> get it out. Though, CF has some really nice list functions, so you might
> not even have to do that. Also, select boxes return comma separated lists
> back to CF when a form is submitted (ditto for checkboxes with the same
> name)... So that might work in your favor.
>
> For structures though, WDDX is your friend, and quite easy to do...
>
> Try <cfwddx action="CFML2WDDX" input="#workweek#" output="workweekXML">
>
> Then stuff workweekXML into the database field.
>
> When you retrieve the data back out of the database, use cfwddx again to
> convert back to a CF structure
>
> <cfwddx action="WDDX2CFML" input="queryname.databaseWDDXField"
> output="localStructVariable">
>
> Just make sure that your db field can handle a decent sized chunk of data,
> especially if you are allowing user defined data in this column as the WDDX
> format can get kinda bloated.
>
>
> -----Original Message-----
> From: Adam Bourg [mailto:[email protected]]
> Sent: Wednesday, April 06, 2011 10:41 AM
> To: cf-talk
> Subject: Storing Arrays or Structures in a database
>
>
> I've got really large sets of data that are related to each day of the week
> and the hour. Rather then creating a massive table containing these values,
> I have stored them in a Structure, but I can't seem to get Structures or
> Arrays to insert into a database. How can I store an array or structure in a
> MS 2008 SQL server?
>
> Array Code:
>
> <cfset name="Adam" />
> <cfscript>
> // 7 days, day 0 = Mon, Day 7 = sun
> workWeek=["t","t","f","t","f","t","f"];
> </cfscript>
>
> <cfinvoke method="injectData" component="anubis">
> <cfinvokeargument name="name" value="#name#" />
> <cfinvokeargument name="workWeek" value="#workWeek#" />
> </cfinvoke>
>
> <cffunction name="injectData" access="public" returntype="void"
> hint="Injects the Employee App form data">
>
> <cfquery datasource="DSN" name="insertData"> INSERT INTO
> mod_employmentAppProfile(
> name,
> workWeekendsEvenings
> )
> VALUES (
> #ARGUMENTS.name#,
> #ARGUMENTS.workWeek#
> )
> </cfquery>
>
>
>
>
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:343580
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm