简体   繁体   English

动态地将列添加到DBIx :: Class ResultSet

[英]Dynamically Adding Columns to a DBIx::Class ResultSet

I have a DBIx::Class object representing an eBay auction. 我有一个代表eBay拍卖的DBIx::Class对象。 The underlying table has a description column which contains a lot of data. 基础表有一个描述列,其中包含大量数据。 The description column is almost never used, so it's not included in the DBIx::Class column list for that table. 几乎从不使用description列,因此它不包含在该表的DBIx::Class列列表中。 That way, most queries don't fetch the auction description data. 这样,大多数查询都不会获取拍卖描述数据。

I do, however, have one script that needs this column. 但是,我有一个需要此列的脚本。 In this one case, I want to access the contents of the description column as I would any other column: 在这种情况下,我想像访问任何其他列一样访问description列的内容:

$auction->description

How can I accomplish this without forcing all other queries to fetch the description column? 如何在不强制所有其他查询获取描述列的情况下完成此操作?

In older versions of DBIx::Class (not sure of the version number), the following used to work: 在旧版本的DBIx::Class (不确定版本号),以下内容曾经起作用:

my $rs = $schema->resultset('Auctions');
my $lots = $rs->search(
   undef,
   { '+select' => 'description', '+as' => 'description' },
);

That doesn't seem to work for row updates under modern versions of DBIx::Class . 在现代版本的DBIx::Class下,这似乎不适用于行更新。 Trying that with an update 尝试更新

$auction->update({ description => '...'})

under DBIx::Class 0.08123 gives the following error: "DBIx::Class::Relationship::CascadeActions::update(): No such column description at ..." DBIx::Class 0.08123下给出以下错误:“DBIx :: Class :: Relationship :: CascadeActions :: update():没有这样的列描述...”

Assuming that the script needing the extra column is running in its own process. 假设需要额外列的脚本在其自己的进程中运行。 You can do something like this: 你可以这样做:

my $rs = $schema->resultset('Auctions');
$rs->result_source->add_columns('description');
YourApp::Schema::Lots->add_columns('description');
YourApp::Schema::Lots->register_column('description');

Of course, that's a global change. 当然,这是全球变化。 After adding the column, other code in the same process will start fetching the description column in queries. 添加列后,同一进程中的其他代码将开始在查询中获取description列。 Not to mention, it's kind of ugly. 更不用说,它有点难看。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM