Thursday, July 16, 2009

Catalyst and DBIX::Class tip

I hadn't been using DBIX::Class with Catalyst very long before I realized that stuffing an array of Row objects into a session was a Bad Idea. a. You probably don't need all that schema and database state info that comes with it just to load your data into your html. b. you probably don't really want to try to stuff a live connection to a database into your session object and c. the things take up a boat load of session memory.

Although the DBIX::Class docs are actually pretty good it took me a while to come up with the answer:

DBIx::Class::ResultClass::HashRefInflator. If all you need is your data stuffed into a hashref and you're not using prefetches, this is what you want to use to keep your results light weight.

Sample code below is simple and works like a charm:

use DBIx::Class::ResultClass::HashRefInflator;

my $rs = $schema->resultset('CD');
$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
while (my $hashref = $rs->next) {
...
}

No comments:

Post a Comment