Re: Heterogeneous collections, variants and has_many
[prev]
[thread]
[next]
[Date index for 2005/03/09]
--- ivor.williams@xxx.xxx wrote:
> 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.
>
You can specify in the third argument of has_many the column name that has the
id. see has_many docs. ex: xx->has_many(foos=> 'FOO', 'foo_id'); If you need
multicolumn key support then see thread on has_many might_have constraints few
weeks ago. The developement version of cdbi has new has_many in it i believe.
I got a link to it a while back from cdbi list. If you can't find it i'll dig
it up.
> Have I misunderstood how to use has_variant?
I've never used has_variant so maybe what i said above won't work if it does
something weird with has_many.
>
> 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.
I use the is_a relationship from cpan for this. Its great. you can say
BAR->create($params) and it creates the THINGY for you first, puts Thingy_id in
associated col in BAR, etc. Its cool.
pjs
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
|
|
Re: Heterogeneous collections, variants and has_many
Peter Speltz 19:45 on 09 Mar 2005
|