简体   繁体   English

如何在Class :: DBI中使用add_to?

[英]How do I use add_to in Class::DBI?

I'm trying to use Class::DBI with a simple one parent -> may chidren relationships: 我正在尝试将Class :: DBI与一个简单的单亲一起使用->可能会减少关系:

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. 我看了经典的Music :: CD示例,但是我无法弄清楚自己做错了什么。

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. 我同意Manni的观点,如果您的程序包声明位于同一文件中,则需要首先定义具有has_a()关系的类。 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. Class :: DBI通常应该能够做正确的事情,只要所有类在“使用”任何其他类之前都继承Class :: DBI。

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. has_many()的第三个arg是外部类中的列,它是此类的外键。 That is, Offer has a CompanyId which points to Company 's CompanyId . 也就是说, OfferCompanyId指向CompanyCompanyId

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 我今天早上打开计算机电源后才意识到:*服务器上的Apache + mod_perl * SMB安装

When I made changes to several files, not all changes seems to be loaded by mod_perl. 当我对几个文件进行更改时,似乎并非所有更改都由mod_perl加载。 Restarting Apache solves the issue. 重新启动Apache可解决此问题。 I've actually seen this kind of issue in the past where the client and SMB server's time are out of sync. 过去,我实际上已经看到过这种问题,客户端和SMB服务器的时间不同步。

The code above works fine with 1 file for each module. 上面的代码对于每个模块只有1个文件有效。

Thank you 谢谢

I really haven't got much experience with Class:DBI, but I'll give this a shot anyway: 我确实对Class:DBI没有太多的经验,但是我还是会尝试一下:

  1. The documentation states that: "the class with the has_a() must be defined earlier than the class with the has_many()". 文档指出:“必须在具有has_many()的类之前定义具有has_a()的类”。
  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. 我找不到任何有关您使用has_a和has_many的方式的引用,其中包含三个参数,在您的情况下始终为'CompanyId'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM