如何与 DBIx::Class 创建嵌套的 has_many 或 Belongs_to 关系?

发布于 2024-08-23 11:52:09 字数 2107 浏览 2 评论 0原文

在我的代码中,我有以下三个类: ForumForum::ThreadForum::Post

我想要做的是创建一个使用 has_many 从 Forum::Post 类到 Forum 类的 own_to-relationship 关系,反之亦然,最好无需为其创建自定义函数。 (诚​​然,这更多的是一种智力练习,而不是技术限制或实际问题,但如果可能的话,我很想知道。)

注释掉的行包含了我对这些关系的意图,但以目前的形式,它们失败了去工作。我查阅了文档,但找不到与此特定案例相关的任何内容。

有什么指点吗?

论坛类:

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;

主题类: 帖子

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;

类:

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: 为了简洁起见,省略了保存实际内容的其他列。

In my code I have three classes as follows: Forum, Forum::Thread and Forum::Post

What I want to do is create a belongs_to-relationship from the Forum::Post class to the Forum class and vice versa with a has_many, preferably without creating a custom function for it. (This is admittedly more of an intellectual exercise than a technical limitation or actual problem, but if it is possible, I would much like to know.)

The commented out lines contain my intention with the relationships, but in their current form, they fail to work. I've poked around in the documentation, but cannot find anything relevant to this specific case.

Any pointers?

The forum class:

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;

The thread class:

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;

The post class:

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: Additional columns to hold actual content were omitted for brevity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妳是的陽光 2024-08-30 11:52:09

看起来嵌套关系是不可能的。 has_many 采用单个外部类,该外部类具有调用类的外键。

好消息是 $forum->threads->posts 返回单个 DBIx::Class::Resultset。在需要之前它不会转换为 SQL,因此当您调用 $forum->threads->posts->all() 甚至类似 $forum->search_lated( 'threads',{},{rows=>25})->posts->all(),它只运行单个查询。

如果您的目标是拥有一个 $post->forum,它始终可以是一个方法:sub forum{$_[0]->thread->forum}

It looks like nested relationships aren't possible. has_many takes a single foreign class, which has a foreign key to the calling class.

The good news is $forum->threads->posts returns a single DBIx::Class::Resultset. It is not translated into SQL until needed, so when you call $forum->threads->posts->all() or even something like $forum->search_related('threads',{},{rows=>25})->posts->all(), it only runs a single query.

If your goal is to have a $post->forum, it can always be a method: sub forum{$_[0]->thread->forum}

轮廓§ 2024-08-30 11:52:09

您使用什么数据库和引擎类型?如果您没有外键(例如,MySQL 中的 myisam 表),那么您必须在关系语句中提供列名称。

What database and engine-type are you using? If you don't have foreign keys (like, for instance, with myisam tables in MySQL) then you'll have to provide the column names in your relationship statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文