[Poop-group] Class::DBI 0.32

Tony Bowden tony at kasei.com
Wed Sep 12 05:01:42 CDT 2001


On Wed, Sep 12, 2001 at 09:38:31AM +0200, Michael G Schwern wrote:
> (Thinking [cascading deletes] through)
> Ok, so that's a one-to-many relationship.  Ummm, I'm trying to think
> of a good example... one person, many email addresses.  If the person
> goes away, their email addresses should, but not vice-versa.  Right.
> And a many-to-many relationship would have to be modeled with a link
> table, so that's not a problem.
> I think that'll work.

hasa + has_list works very well with many-to-many

For example, some client work I did recently handled a sock database (don't ask!):

So, for the 3 tables:

sock (id, name, yada, yada, yada);
colour (colourid, name, etc.);
colour_xref (xrefid, sockid, colour);

we set up:

package Sock;
use base 'SockDBI';
Sock->set_up_table('sock');
Sock->hasa_list('ColourXref', ['sockid'], 'colours');

package ColourXref;
use base 'SockDBI';
ColourXref->hasa('SockColour', 'colour');
sub number { shift->colour->colourid }
sub name   { shift->colour->name }

We can ask for $sock->colours, and then we do a $sock->delete it also
deletes all the ColourXrefs for that sock, but doesn't delete the colour
record, which should survive even if there are no socks of that colour
currently...

All works wonderfully. Throw some Template::Toolkit into the equation and
designers can be passed a sock, and iterate over the colours etc.

Tony




More information about the Belfast-pm mailing list