在我的代碼有三個類,如下所示:Forum
,Forum::Thread
和Forum::Post
如何使用DBIx :: Class創建嵌套的has_many或belongs_to關係?
我想要做的就是創建一個從Forum::Post
類的Forum
類,反之亦然用的has_many一個belongs_to的關係,最好而無需爲其創建自定義功能。 (這當然是更多的智力練習,而不是技術限制或實際問題,但如果可能的話,我很想知道。)
註釋掉的行包含我關於關係的意圖,但是在它們的當前形式,他們失敗了。我在文檔中探索過,但找不到與此特定案例相關的任何內容。
任何指針?
論壇等級:
package Schema::Result::Forum;
use Moose;
extends qw/DBIx::Class/;
__PACKAGE__->load_components (qw/Core/);
__PACKAGE__->table ('forum');
__PACKAGE__->add_columns (
id => {
is_auto_increment => 1,
data_type => 'integer',
},
);
__PACKAGE__->set_primary_key ('id');
__PACKAGE__->has_many (threads => 'Schema::Result::Forum::Thread');
#This is the interesting line
#__PACKAGE__->has_many (posts => 'threads' => 'forums');
1;
Thread類:
package Schema::Result::Forum::Thread;
use Moose;
extends qw/DBIx::Class/;
__PACKAGE__->load_components (qw/Core/);
__PACKAGE__->table ('forum_thread');
__PACKAGE__->add_columns (
id => {
is_auto_increment => 1,
data_type => 'integer',
},
forum => {
data_type => 'integer',
},
);
__PACKAGE__->set_primary_key ('id');
__PACKAGE__->belongs_to (forum => 'Schema::Result::Forum');
__PACKAGE__->has_many (posts => 'Schema::Result::Forum::Post');
1;
Post類:
package Schema::Result::Forum::Post;
use Moose;
extends qw/DBIx::Class/;
__PACKAGE__->load_components (qw/Core/);
__PACKAGE__->table ('forum_post');
__PACKAGE__->add_columns (
id => {
is_auto_increment => 1,
data_type => 'integer',
},
thread => {
data_type => 'integer',
},
);
__PACKAGE__->set_primary_key ('id');
__PACKAGE__->belongs_to (thread => 'Schema::Result::Forum::Thread');
#This is the other interesting line
#__PACKAGE__->belongs_to (forum => 'thread' => 'forum');
1;
PS:附加列,持有的實際內容進行了省略簡潔。
你有什麼期望這個嵌套關係呢?在這些情況下'$ post-> forum'或$ forum-> post'是什麼意思?你不能只使用'$ post-> thread-> forum'和'爲我的$ thread($ forum-> threads){爲我的$ post($ thread-> posts){}}'做你想做的事? – 2010-03-03 17:59:04
@Phillip Potter:我有點擔心過期;我最終想用'$ forum-> posts'實現的是*一個* SQL查詢,它提取所有數據。 $ post-> forum實際上只是$ post-> thread-> forum的簡寫,所以我認爲這不太有趣。 – 2010-03-04 12:17:25
我建議你訂閱DBIx :: Class郵件列表並在那裏詢問。我不確定如何去做你所要求的。 – 2010-03-07 07:58:46