Code: Select all
// This class stores items
abstract BWItemCollection implements Countable, IteratorAggregate
{
public function addItem( IBWItem $item, $qty ) { ... }
public function removeItem( $itemID ) { ... }
public function hasItem( IBWItem $item ) { ... }
.... more misc functions ....
}
// This is the shopping cart
class BWCart extends BWItemCollection
{
public function saveToSession( $sessionVarName ) { ... }
public function loadFromSession( $sessionVarName ) { ... }
}
// Order class
class BWOrder extends BWItemCollection
{
public function loadFromDB( $orderID ) { ... }
public function buildFromCart( BWCart $cart ) { ... }
public function saveToDB() { ... }
public function cancel() { ... }
public function getTracking() { ... }
.... etc. etc. ....
}
// Item Interface
interface IBWItem
{
public function getPrice() ;
public function getName() ;
public function getID() ;
}
// Simple item class, might make it abstract
class BWItem implements IBWItem
{
... implements the interface methods ....
}
// This is a decorator class. It adds a quantity value to any BWItem thats stored in it. Whenever a
// item is added to BWItemCollection it gets put in this wrapper so that the quantity can be tracked
class BWOrderItem implements IBWItem
{
// Set the component
public function __construct( IBWItem $item ) { ... }
public function getQty() { ... }
public function setQty( $qty ) { ... }
public function addQty( $qty ) { ... }
... implements the interface methods and some other stuff ....
}
// A concrete item.
class SomeItem extends BWItem
{
public function getImage() { ... }
public function getThumbnail() { ... }
public function getDescription) { ... }
}
// Some other item with a different table structure
class SomeOtherItem extends BWItem
{
public function getEngineType() { ... }
}I've got it all cool now where I can create items, add them to the cart, save it to a session, loop through the cart, convert it to an order and all that jazz.
The problem is that the different item types will be stored in different tables. There will be a single item table, and that will be linked to the different item types. The idea is I can take my little shopping cart API thing and set it up for a different customer who maybe has 3 or 4 item types and then just create the correct item classes and tables and good to go.
The itemOrder table will store an itemID, qty, orderID.
The item table will have itemID and an itemType?
Then in the example above I would have a SomeItem table with itemID, image, thumbnail, description...
Question Would a factory be a good solution to creating objects from data retrieved from the database? To get items I would select the ItemIDs and types from the items table, then left join all of the different item tables and call the factory on each one like
Code: Select all
$order = new BWOrder() ;
foreach( $items as $item )
{
$order->addItem( BWItem::factory( $item ), $item['qty'] ) ;
}Another question When it comes to saving the records to the db, how should I approach this? There are a few parts to this
- Modifiying items: The site admin can add/delete/modify items. This will involve modifying one item at a time. Should the sql for this be stuck into the different item implementations? Or should it be encapsulated in another class that handles this that takes IBWItem objects and manages records via a definition stored within the item? This one is hurting my head.
- Placing orders: People can place/modify/cancel orders. This will involve modifying many orderItem records at one time. I assume the SQL should go into the BWOrder object as its going to be writing a set of records to only the orderItems table and to the orders table. Does that sound right?
- Loading an order: When loading an order a somewhat large SQL statement will need to be generated utilizing each of the item types so that all the item tables can be joined. My idea was to have a clause for left join be stored in each item type in a static method so that this can be build. How do I know what item classes to call though at run time? Should I use a map or something or manually edit the sql?
Yet one more question I have seen item databases with dynamic properties for products. So you create items not by creating a new class but by creating a definition for that item and storing it in the DB. Then the item object would be build on the fly. Any insight into that or input on if that is a better solution? It sounds like a real pain in the ass.
I really appreciate any input or opinions on this.
edit: I can already see a snafu with my BWOrderItem decorator. It really shouldn't be a decorator but just some object that will return the information needed to create an orderItem record, store the qty, and hold a BWItem. I'll fix that.