Re: has_a and the accessor name

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

From: .--- .- -- . ...
Subject: Re: has_a and the accessor name
Date: 18:02 on 26 Mar 2005
On Sat, 26 Mar 2005 08:38:09 +0100, Josef Chladek <josef@xxxxxxxx.xxx> wrote:
> maybe this is purely trivial, but i can't find a solution:
> 
> i have a ont-to-one relationship between two tables:
> 
> package Newsletter::Main;
> use base 'Newsletter::DBI';
> 
> __PACKAGE__->table( "Newsletter" );
> __PACKAGE__->columns(Primary => qw /id/);
> __PACKAGE__->columns(Essential => qw /email/ );
> __PACKAGE__->columns(Others => qw /no_mails modified md5/ );
> __PACKAGE__->has_a( "id", "Newsletter::Attributes");
> 
> package Newsletter::Attributes;
> use base 'Newsletter::DBI';
> 
> __PACKAGE__->table( "Newsletter_Attributes" );
> __PACKAGE__->columns(Primary => qw /newsletter/ );
> __PACKAGE__->columns(Essential => qw /vorname nachname anrede/ );
> __PACKAGE__->has_a( "newsletter", "Newsletter::Main");
>

your tables seem wrong to me.  has_a indicates that a column in the
current table is a foreign key into another related table...

so your newsletter table needs a "newsletter_attribute" column which
would be the primary key in your Newsletter_Attributes table:

Newsletter->columns(Essential=>qw/email newsletter_attribute/);
Newsletter->has_a( newsletter_attribute=>'Newsletter::Attributes'); 

have a look at the "might_have" declaration:
http://search.cpan.org/~tmtm/Class-DBI-0.96/lib/Class/DBI.pm#might_have

-james

> if i create the main object via
> 
> $cid = Newsletter::Main->create(\%data);
> 
> i then prepare the %data_attributes for the Attributes,
> 
> $data_attributes{newsletter} = $cid;
> $aid = Newsletter::Attributes->create(\%data_attributes);
> 
> everything works fine. but my program is more generalized, so i would
> need something to return me the accessor in Attributes, so i would get
> 'newsletter' in my example.
> 
> it should look like
> my $foreign_accessor =
> Newsletter::Attributes->return_me_the_accessor_of_has_a # which would
> return 'newsletter'
> $data_attributes{$foreign_accessor} = $cid;
> 
> do i have to write a method on my own or have i overlooked something?
> 
> thanks
> josef
> 
> 


        -- 
        .--- .- -- . ... -.-- --- ---

has_a and the accessor name
Josef Chladek 07:38 on 26 Mar 2005

Re: has_a and the accessor name
William Ross 08:46 on 26 Mar 2005

Re: has_a and the accessor name
Josef Chladek 19:18 on 27 Mar 2005

Re: has_a and the accessor name
.--- .- -- . ... 18:02 on 26 Mar 2005

Re: has_a and the accessor name
Peter Speltz 06:23 on 28 Mar 2005

Re: has_a and the accessor name
Josef Chladek 07:50 on 28 Mar 2005

Re: has_a and the accessor name
Peter Speltz 17:59 on 28 Mar 2005

Generated at 14:42 on 11 Apr 2005 by mariachi v0.52