wypm has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm a Perl novice and trying to learn Catalyst. In the application I'm building, I saved some objects from resultset inside one action and redirected to the other function, but it displayed a blank page with exception

Can't call method "class" on an undefined value at /usr/local/lib/perl5/site_perl/5.10.0/DBIx/Class/ResultSourceHandle.pm line 81, at /usr/local/lib/perl5/site_perl/5.10.0/DBIx/Class/Serialize/Storable.pm line 14, at /usr/local/lib/perl5/site_perl/5.10.0/Object/Signature.pm line 19 Example of the codes:
sub foo { ... @some_records = $c->model("DB")->resultset("MyTable")->all; $c->session->{data} = \@some_records; ... $c->redirect->($c->uri_for('do_something')); } sub do_something { $c->stash->{template} = 'do_something.tt2'; } In do_something.tt2: [% FOREACH record IN c.session.data %] [% record.comment_field %] [% END %] etc.
I did some web search and saw some responses that it's because the database connection was reset/lost during redirect, and it was suggested to use DBIx::Class::Schema freeze/thaw so solve the problem. But I have trouble understand the exact syntax to use freeze/thaw in the above codes. Thanks.

Replies are listed 'Best First'.
Re: Catalyst redirect schema freeze/thaw example
by Your Mother (Archbishop) on Jul 16, 2009 at 00:33 UTC

    You can't freeze DBIC objects that way--they lose special meta attributes--and the session isn't really meant for passing around that kind of data. The stash is, though I realize you're redirecting so you have no choice. This tweak should make it work (untested though). I'd question whatever logic/design led you to save a bunch of data in the session and then redirect; it's probably not the best way to accomplish whatever you're trying to do. You'll probably need to install DBIx::Class::ResultClass::HashRefInflator; I'm pretty sure it's not in the core package.

    my $rs = $c->model("DB")->resultset("MyTable")->search(); # This is *probably* what you want and can do instead- # my $rs = $c->model("DB::MyTable")->search(); $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); $c->session->{data} = [ $rs->all ];