Michael - I think your problem is that you're trying to save a complex object into a single database column, which is usually a no-no. What's going in is just the string representation of the pointer to the object, not the data that makes it up.
Your products are going to need to be stored in a separate table. That table will need to have one column for each parameter plus a column to serve as a unique key. Since I think you said your products are highly configurable (and ignoring "true enterprise level data model" for the moment) you'll need to store the product(s) first, and then use the unique key value created on that insertion to store with the quote record in order to tie the two together. If a quote will only ever have one product, you can simply add a column to the table that stores quotes to hold the foreign key to associate it with the previously stored product. If, however, each quote could contain more than one product, you'll need to use what's called a bridge table to store pairs of keys. In this case you'll end up inserting the products and the quote in any order, then inserting records in the bridge table once for each product. Each record in that 3rd table (which only has 2 columns) will contain the unique key for the quote and the unique key for the product. So, to sum up in some bullet points, here we go... * You shouldn't try storing a display object in a database, but store the distinct parameters necessary to recreate a display object, recall them, and rebuild the display each time. (I think this is what you actually meant in your original message, but you just stated it a little ambiguously) * Any time you create an object that needs to be stored in a database, and that object has a parameter that is an object itself (as opposed to a primitive like a string or integer) that 2nd object should probably get a new database table and some foreign key relationship will need to be established to keep the relationship properly defined. Hope this helps ~Jason On Thu, Sep 10, 2009 at 8:01 AM, Hepp, Michael W. <[email protected]>wrote: > Okay, assuming that I am not a complete moron, which I understand might be > a stretch at this point. And assuming that I don't get my jollies by going > around kneeing major deities in the groin... oh sure, occasionally I may > drop kick a minor deity's gonads across the room (we all do that!!), but I > never mess with the big boys. > > So here is what we've got: > We have a company which has a number of products and each of these products > has various configurations. > We have a sales force that has to put several of these products together > into a "quote" for their customers. > We have to be able to save the "quote" and the product configurations (of > which there can be several in a single quote) so that they can be > changed/updated at the customer's request and a new quote submitted. > > So the salesman begins creating a "quote" for the customer by selecting > Wiget A. Widget A has several options. > Option 1: Plastic or stainless steel (two choices) > Option 2: Flip or toggle switches ( 10 choices based on the choice in > Option 1) > Option 3: Green, yellow, purple or blue flashing lights ( 20 choices based > on the choice in Option 1 & 2) > > So we create a Product Object: > package classes.Product > { > import mx.collections.ArrayCollection; > > public class Product{ > > > //****************************************************************************// > //***************************** GETTERS/SETTERS > ******************************// > // Define internal private variables > private var _id:String = new String(); > private var _label:String = new String(); > private var _code:String = new String(); > private var _basePrice:String = new String(); > private var _description:String = new String(); > private var _notes:String = new String(); > private var _image:String = new String(); > private var _optionsData:ArrayCollection = new > ArrayCollection(); > > > > //****************************************************************************// > public function get id():String { > return _id; > } // END get() > > public function set id(value:String):void { > _id = value; > } // END set() > > //---------------------------------- > public function get label():String { > return _label; > } // END get() > > public function set label(value:String):void { > _label = value; > } // END set() > > //---------------------------------- > public function get code():String { > return _code; > } // END get() > > public function set code(value:String):void { > _code = value; > } // END set() > > //---------------------------------- > public function get basePrice():String { > return _basePrice; > } // END get() > > public function set basePrice(value:String):void { > _basePrice = value; > } // END set() > > //---------------------------------- > public function get description():String { > return _description; > } // END get() > > public function set description(value:String):void { > _description = value; > } // END set() > > //---------------------------------- > public function get notes():String { > return _notes; > } // END get() > > public function set notes(value:String):void { > _notes = value; > } // END set() > > //---------------------------------- > public function get image():String { > return _image; > } // END get() > > public function set image(value:String):void { > _image = value; > } // END set() > > //---------------------------------- > public function get optionsData():ArrayCollection { > return _optionsData; > } // END get() > > public function set optionsData(value:ArrayCollection):void > { > _optionsData = value; > } // END set() > > //---------------------------------- > > > //*****************************************************************// > > //*****************************************************************// > // CONSTRUCTOR > public function Product() { > super(); > } // END constructor() > } > } > > Which I consider a pretty basic object (or a POAO if you prefer). Each > option is stored in an array which is added to an arrayCollection ( > optionsData ) and updated as options change. We won't get into how I managed > to extend this class into a ViewStack (it involves stomping on small furry > animals). I just want to be able to set/change this object's properties, > save it to a database and retrieve it. > > When I write to the database after typing everything through my item class, > it appears to save to object to the database... > > private function writeToDatabase():void{ > var productData:Product = new Product(); > productData.id = "123"; > productData.label = "Widget"; > productData.code = "1A2B3C"; > productData.basePrice = "19.99"; > productData.description = "The Rain in Spain."; > productData.notes = "Falls mainly in the plains, but only on > alternate Thursdays"; > productData.image = "productPhoto.jpg"; > productData.optionsData= new ArrayCollection(); > > // itemClass Values > item.status = 1; > item.date = new Date(); > item.name = "test quote"; > item.cartDP = cartDP; > item.productData = productData; > > var params:Array = [ item.status, item.date, item.name, > item.cartDP, item.productData ] > var stmt:String = "INSERT INTO Quote (status, date, name, > cartDP, productData)" + > " VALUES( @status, @date, @name, @cartDP, > @productData)"; > trace (stmt); > sqlQuoteConnection.addItem( stmt, params ); > } > > private function readFromDatabase():void{ > var sqlStatement:String = "SELECT * " + > "FROM Quote WHERE status='1' " + > "ORDER BY date DESC"; > // Add EVENT LISTENER > > sqlQuoteConnection.addEventListener(SQL_ConnectionEvent.READ_COMPLETE, > > openQuoteReadComplete); > // EXECUTE SQL ACTION > sqlQuoteConnection.read(sqlStatement); > } > > I can't get the Object back! Everything else comes back, but for the > product I get the following error: > TypeError: Error #1034: Type Coercion failed: cannot convert obj...@3286cb9to > classes.Product.Product. > > Am I really so far off in my thinking?? I know I am not a guru at this > stuff, which is why I come here to you guys, but I didn't think I was such a > loose cannon that I missed the mark by that much. > > Thanks for your input, > Mike > > > -----Original Message----- > From: [email protected] on behalf of Charlie Hubbard > Sent: Wed 9/9/2009 11:33 AM > To: [email protected] > Subject: Re: [AFFUG Discuss] writing/reading a ViewStack to SQLite > > Holy Jeez! Maybe it's just the way your choosing to think about it that > sounds bad, but persisting UI components to a database is a serious bad > idea. Like kicking Jesus in the nuts bad. > You need to separate out the portions of the UI that you will be saved to > the database from the actual UI. By separating things out more you can > deal > with them independantly rather than one big ball of mud. Pull out data you > want to save from UI elements into a POAO - (plain old actionscript > object). > What does this UI represent in terms of your domain? Is it a Shopping > Cart? Create a class that represents it a shopping cart. What goes in a > shopping cart? An array of LineItems (another class). Any special offers > being applied? What are LineItems? A Quantity, a Product, and Price. > > Once you have your model for the shopping chart together then refactor your > ViewStack into a ShoppingChartView that has setter/getter for the > ShoppingCart object. Override commitProperties to bind your shopping cart > object into the UI controls that make up your ShoppingChartView. Once > you're done with that then you can begin to save the ViewStack's state in > that POAO. > > Now I can ask the ViewStack component to get the ShoppingCart object and it > will return the state of the ViewStack back to me. On the other side the > setter will bind the ShoppingCart object into the ViewStack component > (hence > restoring it's state). That gives me convenient methods to work with the > component at a higher level, and it gives me a barrier between the SQL > statements/Database code and my component. So my database interaction can > be moved to some other location in my program, and my component exists > independent of a database. > > Once you have the POAO together then you can start to save that object to > your database. Sounds like you're encountering some null object, but > without a stack trace it's a little hard to point exactly where you're > going > wrong. But, from the sound of it it's just too jumbled up to make heads or > tails of anything. > > Charlie > > > On Wed, Sep 9, 2009 at 10:31 AM, Hepp, Michael W. > <[email protected]>wrote: > > > > > Greetings, > > > > I am trying to write a ViewStack with various radio buttons to a local > > database and then retrieve it in its saved (persisted) state but I am not > > having any success. Is it even possible to write/read a ViewStack to a > > database? I can write and retrieve strings, numbers, dates, and arrays, > but > > not the ViewStack. > > > > private function writeToDatabase():void{ > > // Type to itemClass > > item.status = 1; > > item.date = new Date(); > > item.name = "test quote"; > > item.cartDP = cartDP; > > item.productData = productVS; > > > > var params:Array = [ item.status, item.date, item.name, > > item.cartDP, item.productData ] > > var stmt:String = "INSERT INTO Quote (status, date, name, > > cartDP, productData)"; > > trace (stmt); > > sqlConnection.insert( stmt, params ); > > } > > > > public function insert( sqlStmt:String, params:Array = null ):void{ > > var sqlInsert:SQLStatement = new SQLStatement(); > > sqlInsert.sqlConnection = sqlConnection; > > > > // ADD EVENT LISTENERS > > sqlInsert.addEventListener(SQLEvent.RESULT, onInsertSuccess); > > sqlInsert.addEventListener(SQLErrorEvent.ERROR, errorHandler); > > > > // ASSIGN PARAMETERS > > if( params ) { > > for( var i:int = 0; i < params.length; i++ ) { > > sqlInsert.parameters[i] = params[i]; > > } > > } > > sqlInsert.itemClass = ItemTO; > > > > // EXECUTE SQL ACTION > > sqlInsert.execute(); > > } // END insert() > > > > And this is all written to my local database, but when I read this back > in > > I get Error #1009: Cannot access a property or method of a null object > > reference for the ViewStack even though there is data being returned from > > the query. I've even tried turning it into a byteArray: > > > > var bytes:ByteArray = new ByteArray(); > > bytes.position = 0; > > bytes.writeObject(productVS); > > > > But when I read it back, even though the ByteArray has data, I can't get > it > > back into a ViewStack > > var bytes:ByteArray = new ByteArray(); > > bytes = event.results[0]["productData"]; > > bytes.position = 0; > > var newVS:Dictionary = bytes.readObject() as ViewStack; > > > > I am really spinning my wheels on this one and could really use some > > ideas/comments on what I am doing wrong or if this is even possible. > > > > Thanks, > > Mike > > > > ------------------------------------------------------------- > > To unsubscribe from this list, simply email the list with unsubscribe in > > the subject line > > > > For more info, see http://www.affug.com > > Archive @ http://www.mail-archive.com/discussion%40affug.com/ > > List hosted by FusionLink <http://www.fusionlink.com> > > ------------------------------------------------------------- > > > > > ------------------------------------------------------------- > > To unsubscribe from this list, simply email the list with unsubscribe in > the subject line > > > > For more info, see http://www.affug.com > > Archive @ http://www.mail-archive.com/discussion%40affug.com/ > > List hosted by http://www.fusionlink.com > > ------------------------------------------------------------- > >
