- result_class can now be specified as a search attribute, attaching it to the returned resultset
- the specified class is now autoloaded via ensure_loaded()
use Scalar::Util ();
use base qw/DBIx::Class/;
-__PACKAGE__->mk_group_accessors('simple' => qw/result_class _source_handle/);
+__PACKAGE__->mk_group_accessors('simple' => qw/_result_class _source_handle/);
=head1 NAME
# see https://bugzilla.redhat.com/show_bug.cgi?id=196836
my $self = {
_source_handle => $source,
- result_class => $attrs->{result_class} || $source->resolve->result_class,
cond => $attrs->{where},
count => undef,
pager => undef,
bless $self, $class;
+ $self->result_class(
+ $attrs->{result_class} || $source->resolve->result_class
+ );
+
return $self;
}
=cut
+sub result_class {
+ my ($self, $result_class) = @_;
+ if ($result_class) {
+ $self->ensure_class_loaded($result_class);
+ $self->_result_class($result_class);
+ }
+ $self->_result_class;
+}
=head2 count
use Test::More qw(no_plan);
use lib qw(t/lib);
use DBICTest;
-use DBIx::Class::ResultClass::HashRefInflator;
my $schema = DBICTest->init_schema();
$schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
# order_by to ensure both resultsets have the rows in the same order
+# also check result_class-as-an-attribute syntax
my $rs_dbic = $schema->resultset('CD')->search(undef,
{
prefetch => [ qw/ artist tracks / ],
{
prefetch => [ qw/ artist tracks / ],
order_by => [ 'me.cdid', 'tracks.position' ],
+ result_class => 'DBIx::Class::ResultClass::HashRefInflator',
}
);
-$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
my @dbic = $rs_dbic->all;
my @hashrefinf = $rs_hashrefinf->all;
select => [qw/name tracks.title tracks.cd /],
as => [qw/name cds.tracks.title cds.tracks.cd /],
order_by => [qw/cds.cdid tracks.trackid/],
+ result_class => 'DBIx::Class::ResultClass::HashRefInflator',
});
-$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
@dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
@hashrefinf = $rs_hashrefinf->all;
# (the TODO block itself contains tests ensuring that the warns are removed)
TODO: {
local $TODO = 'Prefetch of multiple has_many rels at the same level (currently warn to protect the clueless git)';
- use DBIx::Class::ResultClass::HashRefInflator;
#( 1 -> M + M )
my $cd_rs = $schema->resultset('CD')->search ({ 'me.title' => 'Forkful of bees' });
use warnings;
use Test::More;
+use Test::Exception;
use lib qw(t/lib);
use DBICTest;
my $schema = DBICTest->init_schema();
-plan tests => 9;
+plan tests => 12;
{
my $cd_rc = $schema->resultset("CD")->result_class;
+ throws_ok {
+ $schema->resultset("Artist")
+ ->search_rs({}, {result_class => "IWillExplode"})
+ } qr/Can't locate IWillExplode/, 'nonexistant result_class exception';
+
+# to make ensure_class_loaded happy, dies on inflate
+ eval 'package IWillExplode; sub dummy {}';
+
my $artist_rs = $schema->resultset("Artist")
->search_rs({}, {result_class => "IWillExplode"});
is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class');
-
+
+ throws_ok {
+ $artist_rs->result_class('mtfnpy')
+ } qr/Can't locate mtfnpy/,
+ 'nonexistant result_access exception (from accessor)';
+
+ throws_ok {
+ $artist_rs->first
+ } qr/Can't locate object method "inflate_result" via package "IWillExplode"/,
+ 'IWillExplode explodes on inflate';
+
my $cd_rs = $artist_rs->related_resultset('cds');
is($cd_rs->result_class, $cd_rc, 'Correct cd result_class');