开发者

Dynamically Adding Columns to a DBIx::Class ResultSet

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 col开发者_Python百科umn?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜