How to use add_to in Class :: DBI?

I am trying to use Class :: DBI with a simple parent -> possibly a chidren relationship:

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 am trying to add a new entry:

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 classic Music :: CD example, but I can't figure out what I'm doing wrong.

+3
source share
3 answers

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

A :: DBI class should usually be able to do the right thing while all classes inherit from Class :: DBI before using any other classes.

, . arg has_many() - , . Offer CompanyId, Company CompanyId.

+4

, , . , : * Apache + mod_perl * SMB

, , , mod_perl. Apache . ​​ , SMB .

1 .

+2

Class: DBI, :

  • : " has_a() has_many()".
  • I cannot find a link to how you use has_a and has_many with three arguments, which are always "CompanyId" in your case.
+1
source

Source: https://habr.com/ru/post/1702456/


All Articles