Re: Question on how to implement Class::DBI with crossover tables..
[prev]
[thread]
[next]
[Date index for 2005/03/02]
On 2 Mar 2005, at 21:23, d. Taylor Singletary wrote:
> That won't quite work though, beause the Vendor table has no
> reference to CTCTID. The connection to a Contact is through another
> table, one that I'd like to abstract into not having to deal with.
That's exactly what happens in the example I gave: sorry I didn't
explain it more clearly.
If you're using vanilla CDBI, you do have to define a Class::DBI
subclass for your joining (crossover) table, but you don't have to put
very much in it and you don't have to worry about its relationships at
all. For example, you probably already have these two:
package Contact;
Vendor->table('CONTACT');
Vendor->columns(Essential => qw(CTCTID ...));
...
package Vendor;
Vendor->table('VENDOR');
Vendor->columns(Essential => qw(VNDRID ...));
...
You need to add a very simple cdbi class for the crossover table (one
table <-> one class, as always):
package Vendor::Contact;
Vendor::Contact->table('XCONVEN');
Vendor::Contact->columns(Essential => qw(XCONVENID CTCTID VNDRID));
1;
And then you can add this to your Vendor class:
Vendor->has_many(contacts => [ 'Vendor::Contact' => 'CTCTID' ]);
after which you just need to write:
my @contacts = Vendor->retrieve($vndrid)->contacts;
to get the right list of Contact objects.
I expect the other approaches mentioned here will make even this little
bit of work go away, but if you're new to CDBI it's probably a good
idea to work with its basic structures before bringing too many helpers
in.
best
will
> The normal has_many would work great if the Vendor table had that
> connection, but it doesn't. What I want to do is abstract it far
> enough that I can treat the Vendor::Contact class as if it had that
> relationship.
>
> Right now, I could do this:
>
> Vendor->has_many(crossovers=>['Vendor::Crossover'=>'VNDRID']);
>
> and
>
> Vendor::Crossover->has_many(contacts=>['Vendor::Contact'=>'CTCTID']);
>
> but I want the crossover table to be automatically populated just by
> editing/adding the Vendor::Contact and Vendor records.
>
> Your example would work fine otherwise. Thank you.
>
> d. Taylor Singletary
>
> On Wed, 2005-03-02 at 21:01 +0000, William Ross wrote:
>
>
> The other options mentioned here will no doubt offer more, but your
> immediate need is more easily met like this:
>
> Vendor->has_many(contacts => [ 'Vendor::Contact' => 'CTCTID' ]);
>
> It's in the Class::DBI docs under relationships/has_many/mapping. I
> think you still need to have a Vendor::Contact package in your tree,
> but you don't have to worry about its relationships or even columns
> unless you decide to start holding more information there.
>
> best
>
> will
>
>
>
>
> >
> > Is it possible with Class::DBI to semi-transparently manage the
> > crossover tables and still assert relationships like
> Vendor::Contact,
> > Customer::Contact? How is that done? When accessing these objects I
> > don't want to have to think about maintaining the relationships in
> the
> > crossover tables at all. Customers can have many contacts, Vendors
> can
> > have many contacts.
> >
> > Thanks for a push in the right direction on this.
> >
> > d. Taylor Singletary
>