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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.