|
From: Chris W. <ch...@cw...> - 2002-01-14 13:26:38
|
On Sun, 2002-01-13 at 21:18, Victor Piterbarg wrote:
> I am writing a product system which is turning out to be fairly complex. One
> of the features I want to implement is product dependecy, i.e you can't buy
> X without buying Y as well. Now to do this I would need to link a product
> object to a bunch of other product objects. However, using the current
> links_to implementation, I wouldn't be able to this in a straighforward
> manner because links to expects to see the ID's from the respective objects
> that are being linked. I can't do that, since the ID fields in this case
> would be the same -- can't make a table like that.
>
> I could do this in a round-about way by creating a wrapper object, which
> would contain a single product_id (named as something else, like
> product_id_dep). Then I would create a links_to relationship from my real
> product object, to a bunch of the wrapper objects. This would waste space
> and time, however.
>
> Now, I have another hypothetical question. Let's say I also wanted to link
> and product to a list of products which can not be purchased at the same
> time as the original product. Currently I would have to create another
> wrapper object for this.
>
> So, am I missing something? Or should I just go with the wrapper objects?
> Thanks.
In my experience, business logic tasks like this should be done with
custom rather than generated code. It's easy to create a custom linking
method that fetches required relationships from a linking table, where
you have something like:
product --> product_link
product_id product_id
required_product_id
And then create a method like:
sub get_required_products {
my ( $self ) = @_;
my $where = 'product_link.product_id = ? AND ' .
'product_link.required_product_id = product.product_id';
return $self->fetch_group({
from => [ $self->table_name, 'ProductLink' ],
where => $where,
value => [ $self->id ] });
}
And then later on in your process if you need to modify how these
relationships are created/fetched -- e.g., you order them based on
strength, or what other products are in the cart, or whatever -- then
you have one place to do it and an API already in place.
Hope I understood your question correctly.
Later,
Chris
--
Chris Winters (ch...@cw...)
Building enterprise-capable snack solutions since 1988.
|