简体   繁体   English

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

[英]How do I add relationships at runtime using DBIx::Class and Catalyst?

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. 由于我只是在运行时确定这一点,因此我无法在模式模块中指定has_many或belongs_to关系以进行启动。

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 . 因此,列fk0将是映射到位置主键id的外键。

I know there must be a re-registration to allow future access to the relationship but I can't figure it out. 我知道必须重新注册以允许将来访问这种关系,但我无法弄明白。

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. 至少不会丢弃任何现有的DBIC对象,并从头开始重新创建它们。 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. DBIx :: Class :: ResultSet :: View模块可以通过让您执行任意代码,但将结果检索为DBIx对象,提供您正在寻找的内容的粗略近似值。

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. 我对这类事情的一般看法是,任何抽象层(和ORM是一个抽象层),都是为了让生活更轻松。 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. 出于这个原因,我建议您使用DBI,正如您在其中一条评论中所建议的那样。 I suspect it will make your life much easier in this case. 我怀疑在这种情况下它会让你的生活更轻松。

I've done this by calling the appropriate methods on the relevant result sources, eg $resultset->result_source-><relationship method> . 我通过在相关结果源上调用适当的方法来完成此操作,例如$resultset->result_source-><relationship method> It does work even in an active application. 它甚至在活动的应用程序中也能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止DBIx :: Class :: Schema :: Loader在Catalyst中自动添加InflateColumn :: DateTime? - How do I prevent DBIx::Class::Schema::Loader from automatically adding InflateColumn::DateTime in Catalyst? 催化剂:如何使DBIx :: Class :: Schema :: Loader :: make_schema_at()静态保留大小写? - Catalyst: How do I make DBIx::Class::Schema::Loader::make_schema_at() static preserve case? 使用关系搜索DBIx :: Class - Using relationships to search in DBIx::Class 如何在Catalyst中的DBIx :: Class上设置SQL选项 - How to set sql options on DBIx::Class in Catalyst 如何在DBIx:Class中执行“或”和“与”操作? - How do I do WHERE ‘OR’ and “AND’ with DBIx:Class? Perl with Catalyst和DBIx :: Class - 如何在继承树中加载一组类? - Perl with Catalyst and DBIx::Class - How to load a sets of classes in an inheritance tree? 在DBIx :: Class中注入关系 - Injecting relationships in DBIx::Class 如何使用除`=`之外的其他运算符来创建DBIx :: Class连接表? - How do I make DBIx::Class join tables using other operators than `=`? Catalyst中的DBIx :: Class升级(DBIx :: Class :: Schema :: Loader) - DBIx::Class upgrade in Catalyst (DBIx::Class::Schema::Loader) 如何使用Test :: DBIx :: Class为测试套件正确加载fixture? - How do I load fixtures correctly for a test suite using Test::DBIx::Class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM