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

发布于 2024-10-05 21:19:32 字数 283 浏览 0 评论 0原文

我有一个代表 eBay 拍卖的 DBIx::Class 对象。基础表有一个描述列,其中包含大量数据。描述列几乎从未使用过,因此它不包含在该表的 DBIx::Class 列列表中。这样,大多数查询就不会获取拍卖描述数据。

不过,我确实有一个脚本需要本专栏。在这种情况下,我想像访问任何其他列一样访问描述列的内容:

$auction->description

如何在不强制所有其他查询获取描述列的情况下完成此操作?

I have a DBIx::Class object representing an eBay auction. 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. 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:

$auction->description

How can I accomplish this without forcing all other queries to fetch the description column?

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

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

发布评论

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

评论(1

你げ笑在眉眼 2024-10-12 21:19:32

在旧版本的 DBIx::Class 中(不确定版本号),以下内容曾经有效:

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

这似乎不适用于现代版本的 DBIx:: 下的行更新:类。尝试在 DBIx::Class 0.08123 下进行更新

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

会出现以下错误:“DBIx::Class::Relationship::CascadeActions::update(): No such column description at ...”

假设需要额外列的脚本正在其自己的进程中运行。你可以这样做:

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

当然,这是一个全球性的变化。添加该列后,同一进程中的其他代码将开始在查询中获取 description 列。更不用说,它有点丑陋。

In older versions of DBIx::Class (not sure of the version number), the following used to work:

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. 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 ..."

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. Not to mention, it's kind of ugly.

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