如何使用 DBIx::Class 和 Catalyst 在运行时添加关系?

发布于 2024-08-21 13:16:10 字数 417 浏览 4 评论 0原文

在我正在构建的应用程序中,用户可以指定表之间的关系。

由于我仅在运行时确定这一点,因此我无法在启动时的模式模块中指定 has_many 或 Been_to 关系。

所以给定两个表;系统和地点,我想添加它们之间的关系以连接记录。

我有以下解决方案的一部分:

$rs = $c->model('DB::system')->result_source;
$rs->add_relationship('locations','DB::place',{'foreign.fk0' => 'self.id'});

因此列 fk0 将是映射到位置主键 id 的外键。

我知道必须重新注册才能允许将来访问该关系,但我无法弄清楚。

In the application I am building, users can specify relationships between tables.

Since I only determine this at runtime, I can't specify has_many or belongs_to relationships in the schema modules for startup.

So given two tables; system and place, I would like to add the relationship to join records between them.

I have part of the solution below:

$rs = $c->model('DB::system')->result_source;
$rs->add_relationship('locations','DB::place',{'foreign.fk0' => 'self.id'});

So the column fk0 would be the foreign key mapping to the location primary key id.

I know there must be a re-registration to allow future access to the relationship but I can't figure it out.

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

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

发布评论

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

评论(2

雨落□心尘 2024-08-28 13:16:11

我不相信您可以在应用程序运行后重新定义这些关系。至少在不丢弃任何现有 DBIC 对象并从头开始重新创建它们的情况下是这样。我怀疑,到那时,重新启动您的应用程序会更容易。

如果您满足在编译时动态定义这些内容,这是可能的......我们在我们的一个应用程序中做了类似的事情。

如果这对您有用,我可以提供一些示例代码。

DBIx::Class::ResultSet ::View 模块可以让您执行任意代码,但将结果作为 DBIx 对象检索,从而提供您正在寻找的内容的粗略近似值。

我对此类事情的一般看法是,任何抽象层(ORM 就是一个抽象层)都是为了让生活变得更轻松。当它妨碍你的应用程序做它想做的事情时,它不再让生活变得更容易,并且应该被丢弃(对于特定用途 - 不一定对于每个用途)。出于这个原因,我建议使用 DBI,正如您在评论之一中所建议的那样。我怀疑在这种情况下这会让你的生活变得更轻松。

I don't believe you can re-define these relationships after an application is already running. At least not without discarding any existing DBIC objects, and re-creating them all from scratch. At that point, it would be easier to just re-start your application, I suspect.

If you're content defining these things dynamically at compile time, that is possible... we do something similar in one of our applications.

If that would be useful to you, I can provide some sample code.

The DBIx::Class::ResultSet::View module might provide a rough approximation of what you're looking for, by letting you execute arbitrary code, but retrieving the results as DBIx objects.

My general opinion on things like this, is that any abstraction layer (and an ORM is an abstraction layer), is intended to make life easier. When it gets in the way of making your application do what it wants, it's no longer making life easier, and ought to be discarded (for that specific use--not necessarily for every use). For this reason, I would suggest using DBI, as you suggested in one of your comments. I suspect it will make your life much easier in this case.

甜心小果奶 2024-08-28 13:16:11

我通过在相关结果源上调用适当的方法来完成此操作,例如 $resultset->result_source->。即使在活动的应用程序中它也能工作。

I've done this by calling the appropriate methods on the relevant result sources, e.g. $resultset->result_source-><relationship method>. It does work even in an active application.

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