Heterogeneous collections, variants and has_many

[prev] [thread] [next] [Date index for 2005/03/09]

From: ivor.williams
Subject: Heterogeneous collections, variants and has_many
Date: 11:22 on 09 Mar 2005
Definition: A heterogeneous collection is a list (or an array) of objects, which don't all belong to the same class.

I have a table called FOO with a column called link. This column can contain an ID into the table BAR or an ID into the table BAZ. There is also a mechanism in place for allocating unique IDs across the whole database. I am not at liberty to change this schema as it is part of an existing live application.

I'm looking at Class::DBI::Relationship::HasVariant, which looks like it should help, but I'm getting stuck with has_many.

package FOO;
__PACKAGE__->set_up_table("FOO");
__PACKAGE__->has_variant( link => 'Transformer');

package Transformer;
sub inflate {
    my $id = shift;

    BAR->retrieve($id) || BAZ->retrieve($id);
}

sub deflate {
    shift->id
}

package BAR;
__PACKAGE__->set_up_table("BAR");
__PACKAGE__->has_many( foos => 'FOO');

package BAZ;
__PACKAGE__->set_up_table("BAZ");
__PACKAGE__->has_many( foos => 'FOO');


The trouble is that table FOO doesn't have a has_a relationship with either
BAR or BAZ.

Have I misunderstood how to use has_variant?

Can anybody suggest a better way of doing this, before I get embroiled in writing a new relationship module?

From a modelling point of view, it would be nice to superclass BAR and BAZ into a common base class (let's call it Thingy). Then I could describe FOO->link as having an id for a Thingy.

Thoughts please...

Heterogeneous collections, variants and has_many
ivor.williams 11:22 on 09 Mar 2005

Generated at 08:54 on 14 Mar 2005 by mariachi v0.52