如何建立具有固定连接条件的 DBIx::Class 关系?
我们有一个链接表,可以在一侧处理多种类型的对象,但我无法弄清楚如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该创建一个包装器方法,用所需参数调用关系:
如果您有多个需要具有这种连接逻辑的表,那么这将很容易变成 DBIx::Class 组件。
You should just make a wrapper method that calls the relationship with the required arguments:
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.
它可以在 has_many 调用中指定,如下所示:
请参阅: DBIx::Class Cookbook
It can be specified in the has_many call like so:
See: DBIx::Class Cookbook