tags:

views:

181

answers:

3

I'm trying to use Class::DBI with a simple one parent -> may chidren relationships:

Data::Company->table('Companies');
Data::Company->columns(All => qw/CompanyId Name Url/);
Data::Company->has_many(offers => 'Data::Offer'=>'CompanyId'); # =>'CompanyId'

and

Data::Offer->table('Offers');
Data::Offer->columns(All => qw/OfferId CompanyId MonthlyPrice/);
Data::Offer->has_a(company => 'Data::Company'=>'CompanyId');

I try to add a new record:

my $company = Data::Company->insert({ Name => 'Test', Url => 'http://url' });
my $offer = $company->add_to_offers({  MonthlyPrice => 100 });

But I get:

Can't locate object method "add_to_offers" via package "Data::Company"

I looked at the classical Music::CD example, but I cannot figure out what I am doing wrong.

+1  A: 

I really haven't got much experience with Class:DBI, but I'll give this a shot anyway:

  1. The documentation states that: "the class with the has_a() must be defined earlier than the class with the has_many()".
  2. I cannot find any reference to the way you are using has_a and has_many with three arguments which is always 'CompanyId' in your case.
innaM
+3  A: 

I agree with Manni, if your package declarations are in the same file, then you need to have the class with the has_a() relationship defined first. Otherwise, if they are in different source files, then the documentation states:

Class::DBI should usually be able to do the right things, as long as all classes inherit Class::DBI before 'use'ing any other classes.

As to the three-argument form, you are doing it properly. The third arg for has_many() is the column in the foreign class which is a foreign key to this class. That is, Offer has a CompanyId which points to Company's CompanyId.

Adam Bellaire
Can you point me to the docs for the third argument?
innaM
It's actually in the same place as what you saw, the very last line in the section on has_many: http://search.cpan.org/dist/Class-DBI/lib/Class/DBI.pm#has_many
Adam Bellaire
A: 

Thank you

Well, the issue was actually not my code, but my set up. I realized that this morning after powering on my computer: * Apache + mod_perl on the server * SMB mount

When I made changes to several files, not all changes seems to be loaded by mod_perl. Restarting Apache solves the issue. I've actually seen this kind of issue in the past where the client and SMB server's time are out of sync.

The code above works fine with 1 file for each module.

Thank you

Julien