> 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.

Hahaha ;-)

The reason your getting a casting exception has to do with how you're
reading the objects back out of SQLite.  If you don't specify an itemClass
you'll get a plain old object back.  Since that can't be cast as a Product
bang exception.  Just set the itemClass = Product on the sql statement
before you execute your read.  That will bring back  your Product.

Couple of things to remember when working with databases.  As Jason pointed
out you have several complex relationships.  Databases don't do complex
relationships within a single table (uggghh I know).  So those relationships
have to go in their own table, and refer to the other table through a
foreign key relationship.  (i.e. many to one, one to many, many to many).
So where ever you have arrays you'll need to break those out into a
one-to-many or many-to-many relationships.  Complex objects are either
many-to-one or one-to-one.  There are loads of ways to map objects to
database tables, but I like to follow these simple(r) rules:

  * One object (class) maps to one table.
  * Each field that is a simple relationship has one column in the table.
  * All tables representing objects have a unique primary key separate from
the data named "id" (auto-increment or guid).
  * Any foreign key is called "object_name_id" where object_name is a
placeholder for the singular table name (Book class -> books table ->
book_id foreign key).
   * Complex relationships are always:
      * one-to-one - foreign key on both tables.
      * many-to-one and one-to-many foreign key only on unmanaged side of
the relationship.  If a Book has an author then he has an author_id column
        since an author can pen many books.  Delete a book, but author
should not be deleted hence Book->Author is unmanaged.
      * many-to-many use a join table.  A single table with two foreign keys
to other tables.
   * Inherritance is always Single Table Inherritance (if you don't have
inheritance don't worry about this).  It's a little too complex to explain
right here.

So now the problem just exploded on you.  Sorry, but that's what databases
do to you.  Should we all just use OODBs?  Maybe.  Good luck.

Charlie

On Thu, Sep 10, 2009 at 8:01 AM, Hepp, Michael W.
<[email protected]>wrote:

> 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
>
> -------------------------------------------------------------
>
>

Reply via email to