horshack has asked for the wisdom of the Perl Monks concerning the following question:

I am struggeling with Class::DBI. When the program runs I get this error:

Can't locate object method "kontakte" via package "Customercare::Kundenkontakte" at cc.pl line x.

What I really want is find a list of customer which had contact with my company. Like this:

01. Libary X
talk
postcard

02. Library Y
postcard

But I thought it's better to retrieve that list by asking for the contacts first. Can you help me?
Thanks,
Horshack

#!/usr/bin/perl use strict; # Buechereien -> german for Libraries # Kundenkontakte -> german for customer-contacts # use Customercare::Buechereien; use Customercare::Kundenkontakte; my @kk = Customercare::Kundenkontakte->retrieve_all; my $i = 0; foreach (@kk) { last if $i++ > 10; printf "%02d. %s\n", $i, $_->customer->plzort; # This is where the problem occurs: foreach ( $_->kontakte ) { printf " %s\n", $_->aktion_kurz; } } package Customercare::Buechereien; use strict; use base 'Customercare::DBI'; __PACKAGE__->set_up_table("buechereien"); __PACKAGE__->has_many( # This will create a new Method named "kontakte" "kontakte", 'Customercare::Kundenkontakte' => "customer"); 1; package Customercare::Kundenkontakte; use strict; use base 'Customercare::DBI'; __PACKAGE__->set_up_table("kundenkontakte"); __PACKAGE__->has_a( customer => 'Customercare::Buechereien' ); 1; These are the tabledefinitions: CREATE TABLE buechereien ( customer varchar(50) NOT NULL default '', anschrift1 varchar(100) default NULL, plzort varchar(100) default NULL, tel varchar(50) default NULL, PRIMARY KEY (customer) ) TYPE=MyISAM; CREATE TABLE kundenkontakte ( lfdnr int(11) NOT NULL auto_increment, customer varchar(50) NOT NULL default '', aktion_kurz varchar(50) NOT NULL default '', PRIMARY KEY (lfdnr) ) TYPE=MyISAM;

Replies are listed 'Best First'.
Re: has_many in Class:DBI
by saintmike (Vicar) on Feb 16, 2004 at 00:18 UTC
    Your Customercare::Buechereien package defines a has_many relationship to Customercare::Kundenkontakte -- but the application complains about Customercare::Kundenkontakte not having the desired relationship.

    Shouldn't it be the other way around, that the has_many relationship is defined in Customercare::Kundenkontakte?

Re: has_many in Class:DBI
by cees (Curate) on Feb 16, 2004 at 02:46 UTC

    I don't think your problem is with Class::DBI, it is with perl. You have two nested foreach loops, and both use $_. Try the following and see what happens:

    foreach my $buechereien (@kk) { last if $i++ > 10; printf "%02d. %s\n", $i, $buechereien->customer->plzort; # This is where the problem occurs: foreach my $kontakte ( $buechereien->kontakte ) { printf " %s\n", $kontakte->aktion_kurz; } }

    - Cees

      You were both right. A nested double $_ and I tried to solve the problem down->up instead up->down. Thanks for your appreciated help! Horshack