I've gotten stuck on a problem with code that works perfectly, but is an ugly mess. Here's the general issue: we have products with three types of monetary values: cost, sale price and list price. More price types will be added in the future. However, due to some complications with our price model, we've moved these out of the product table into their own table. We do this because we have various "price models" that alter the price depending upon the product and the customer. Thus, we have three tables: a product table, a price_type table and a product_prices table with an aggregate key of the product id and the price type id. It's getting the price type id which is tricky.
In the price types table, we currently have the following data:
price_type_id price_type_label 1 Sale Price 2 List Price 3 Cost
The problem is, how do we figure out which type is which? As a temporary hack, I've done the following in our price type object:
sub SALE_PRICE_ID {1} sub LIST_PRICE_ID {2} sub COST_ID {3}
In the product object, I have the following:
sub cost { return $_[0]->get_price(PriceType->COST_ID); } sub list_price { return $_[0]->get_price(PriceType->LIST_PRICE_ID); } sub sale_price { return $_[0]->get_price(PriceType->SALE_PRICE_ID); } sub get_price { my ($self, $id) = @_; return unless $id and $id =~ /^[[:digit:]]+$/; my $price_type = PriceType->retrieve($id); $self->_croak "PriceType ID ($id) unknown" unless $price_type; my $sth = $self->sql_find_price; $sth->execute($self->id, $id); my ($price) = $sth->fetchrow_array; $sth->finish(); return $price; }
That is not terribly maintainable. I don't want to hardcode this data into the price types object, nor do I want code that requires that I write new object methods when I add another row to a database. If I can't figure out a better way of dealing with this, I'm going to have a multi-tiered system where my presentation layer is going to be coupled with my database layer!
Eagerly awaiting any and all advice :)
Cheers,
Ovid
New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)
In reply to Database design and Class::DBI by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |