Re: Question on how to implement Class::DBI with crossover tables..
[prev]
[thread]
[next]
[Date index for 2005/03/02]
--=-rNHLayg8PNa2TfkkdsuC
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Thanks a lot for clearing that up. Now it makes a lot more sense.
Thanks!
d. Taylor Singletary
On Wed, 2005-03-02 at 22:05 +0000, William Ross wrote:
>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
>
>
--=-rNHLayg8PNa2TfkkdsuC
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.5.6">
</HEAD>
<BODY>
Thanks a lot for clearing that up. Now it makes a lot more sense. <BR>
<BR>
Thanks!<BR>
<BR>
d. Taylor Singletary<BR>
<BR>
On Wed, 2005-03-02 at 22:05 +0000, William Ross wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
<FONT COLOR="#000000">On 2 Mar 2005, at 21:23, d. Taylor Singletary wrote:</FONT>
<FONT COLOR="#000000">> That won't quite work though, beause the Vendor table has no </FONT>
<FONT COLOR="#000000">> reference to CTCTID. The connection to a Contact is through another </FONT>
<FONT COLOR="#000000">> table, one that I'd like to abstract into not having to deal with.</FONT>
<FONT COLOR="#000000">That's exactly what happens in the example I gave: sorry I didn't </FONT>
<FONT COLOR="#000000">explain it more clearly.</FONT>
<FONT COLOR="#000000">If you're using vanilla CDBI, you do have to define a Class::DBI </FONT>
<FONT COLOR="#000000">subclass for your joining (crossover) table, but you don't have to put </FONT>
<FONT COLOR="#000000">very much in it and you don't have to worry about its relationships at </FONT>
<FONT COLOR="#000000">all. For example, you probably already have these two:</FONT>
<FONT COLOR="#000000">package Contact;</FONT>
<FONT COLOR="#000000">Vendor->table('CONTACT');</FONT>
<FONT COLOR="#000000">Vendor->columns(Essential => qw(CTCTID ...));</FONT>
<FONT COLOR="#000000">...</FONT>
<FONT COLOR="#000000">package Vendor;</FONT>
<FONT COLOR="#000000">Vendor->table('VENDOR');</FONT>
<FONT COLOR="#000000">Vendor->columns(Essential => qw(VNDRID ...));</FONT>
<FONT COLOR="#000000">...</FONT>
<FONT COLOR="#000000">You need to add a very simple cdbi class for the crossover table (one </FONT>
<FONT COLOR="#000000">table <-> one class, as always):</FONT>
<FONT COLOR="#000000">package Vendor::Contact;</FONT>
<FONT COLOR="#000000">Vendor::Contact->table('XCONVEN');</FONT>
<FONT COLOR="#000000">Vendor::Contact->columns(Essential => qw(XCONVENID CTCTID VNDRID));</FONT>
<FONT COLOR="#000000">1;</FONT>
<FONT COLOR="#000000">And then you can add this to your Vendor class:</FONT>
<FONT COLOR="#000000">Vendor->has_many(contacts => [ 'Vendor::Contact' => 'CTCTID' ]);</FONT>
<FONT COLOR="#000000">after which you just need to write:</FONT>
<FONT COLOR="#000000">my @contacts = Vendor->retrieve($vndrid)->contacts;</FONT>
<FONT COLOR="#000000">to get the right list of Contact objects.</FONT>
<FONT COLOR="#000000">I expect the other approaches mentioned here will make even this little </FONT>
<FONT COLOR="#000000">bit of work go away, but if you're new to CDBI it's probably a good </FONT>
<FONT COLOR="#000000">idea to work with its basic structures before bringing too many helpers </FONT>
<FONT COLOR="#000000">in.</FONT>
<FONT COLOR="#000000">best</FONT>
<FONT COLOR="#000000">will</FONT>
</PRE>
</BLOCKQUOTE>
<BR>
</BODY>
</HTML>
--=-rNHLayg8PNa2TfkkdsuC--