[Dbix-class] Is it possible to use "dumb" result classes with DBIx::Class?

Lasse Makholm lasse at unity3d.com
Thu Apr 9 13:58:38 GMT 2015


I'm using DBIx::Class::ResultClass::HashRefInflator in some places where
full row object inflation is too slow. That's fine.

What I'd really like to do though, is bless the resulting hashrefs into a
"light weight" result class that provides a few convenience methods but
knows little to nothing about DBIx::Class.

Trying something like:

my $rs = $schema->resultset('MyTable')->search(...);
$rs->result_class('MyApp::Schema::DumbResult::MyTable');
while (my $thingy = $rs->next) {

# do, do, do

}


with something like:

package MyApp::Schema::DumbResult::MyTable;

use strict;
use warnings;

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

sub inflate_result
{
my $class = shift;
my $result = DBIx::Class::ResultClass::HashRefInflator->inflate_result(@_);
return bless $result, $class;
}

sub TO_JSON
{
my $self = shift;
return { %$self };
}

1;


...breaks for a variety of reasons it seems, because DBIx::Class wants to
do things like setting a result source instance on the result object, etc...

Is there an (easy) make this work? Or is it just in general a horrible idea?

Of course I can just do:

$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my @stuff = map { bless $_, MyApp::Schema::DumbResult::MyTable } $rs->all;

...but the other way is more convenient...

Or is it more sane to subclass the ResultSet and override first(), all()
and next() to set the result_class and bless the resulting hashrefs?

Any input appreciated?

/L
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20150409/fbf203f3/attachment.htm>


More information about the DBIx-Class mailing list