如何建立具有固定连接条件的 DBIx::Class 关系?

发布于 2024-07-14 19:09:11 字数 1645 浏览 6 评论 0原文

我们有一个链接表,可以在一侧处理多种类型的对象,但我无法弄清楚如何使用 has_many 从这些对象之一获取链接表。

示例:链接表包含:

id link_id link_table 资源_id 
  1 1 页 3 
  2 1 页 5 
  3 2 第 3 页 
  4 1 not_page 1 
  

从资源端构建关系非常简单:

Resource->has_many(links => 'Link', 'resource_id');

但我无法从页面端获取对应关系:

Page->has_many(links => 'Link', 'link_id');

获取 not_page 链接会

Page->has_many(links => 'Link', {'foreign.link_id' => 'self.id', 'foreign.link_table' => 'page'});

给出“Invalid rel cond val page”错误(这对于我)。

Page->has_many(links => 'Link', {'foreign.link_id' => 'self.id', 'foreign.link_table' => '"page"'});

给出“无效的 rel cond val“页面””错误。 扔反斜杠没有帮助。

DBIx::Class::Relationship::Base说:

条件必须是 SQL::Abstract 风格的表示表之间的连接

和我从那里尝试了各种不同的选择,例如:

Page->has_many(links => 'Link', {'foreign.link_id' => 'self.id', 'foreign.link_table' => {'=', 'page'}});

但没有任何成功。

如果我向页表添加另一个字段,该字段始终包含值“page”,我可以这样做,

Page->has_many(links => 'Link', {'foreign.link_id' => 'self.id', 'foreign.link_table' => 'self.what_table_am_i'});

但这并不是最佳解决方案。

将链接表拆分为每种类型的单独一个可能是一种可能性,但这是一个正在考虑适应 DBIx::Class 的现有项目,并且可能还有其他地方将一个表拆分为多个其他表更方便麻烦大于其价值。

We have a link table that can handle multiple types of object on one side, and I can't work out how to get from one of these objects to the link table using has_many.

Example: link table contains:

id link_id link_table resource_id
1  1       page       3
2  1       page       5
3  2       page       3
4  1       not_page   1

Building the relationship from the resource side is easy enough:

Resource->has_many(links => 'Link', 'resource_id');

but I haven't been able to get the corresponding relationship from the page side:

Page->has_many(links => 'Link', 'link_id');

would get the not_page link

Page->has_many(links => 'Link', {'foreign.link_id' => 'self.id', 'foreign.link_table' => 'page'});

gives an 'Invalid rel cond val page' error (which was not that surprising to me).

Page->has_many(links => 'Link', {'foreign.link_id' => 'self.id', 'foreign.link_table' => '"page"'});

gives an 'Invalid rel cond val "page"' error. Throwing backslashes in didn't help.

DBIx::Class::Relationship::Base says:

The condition needs to be an SQL::Abstract-style representation of the join between the tables

and I have tried various different options from there, such as:

Page->has_many(links => 'Link', {'foreign.link_id' => 'self.id', 'foreign.link_table' => {'=', 'page'}});

but without any success at all.

If I added another field to the page table which always contains the value 'page' I could do

Page->has_many(links => 'Link', {'foreign.link_id' => 'self.id', 'foreign.link_table' => 'self.what_table_am_i'});

but that's hardly an optimal solution.

Splitting the link table into a separate one for each type may be a possibility, but this is an existing project that is being considered for adaptation to DBIx::Class, and there may be other places where splitting a table into multiple other tables is more hassle than it's worth.

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

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

发布评论

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

评论(2

醉梦枕江山 2024-07-21 19:09:11

您应该创建一个包装器方法,用所需参数调用关系:

Page->has_many(__all_links => 'Link', 'link_id');

sub links {
    shift->__all_links({link_table => 'page'});
}

如果您有多个需要具有这种连接逻辑的表,那么这将很容易变成 DBIx::Class 组件。

You should just make a wrapper method that calls the relationship with the required arguments:

Page->has_many(__all_links => 'Link', 'link_id');

sub links {
    shift->__all_links({link_table => 'page'});
}

This would be pretty easy to turn into a DBIx::Class component if you have multiple tables that need to have this kind of join logic.

寄居人 2024-07-21 19:09:11

它可以在 has_many 调用中指定,如下所示:

Page->has_many(links => 'Link', 'link_id',
                    { where => { link_table => 'page'} });

请参阅: DBIx::Class Cookbook

It can be specified in the has_many call like so:

Page->has_many(links => 'Link', 'link_id',
                    { where => { link_table => 'page'} });

See: DBIx::Class Cookbook

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