1 package DBIx::Class::ResultSet::HashRef;
6 use base qw( DBIx::Class::ResultSet );
7 use DBIx::Class::ResultClass::HashRefInflator;
9 our $VERSION = '1.002';
13 DBIx::Class::ResultSet::HashRef - Adds syntactic sugar to skip the fancy objects
17 # in your resultsource class
18 __PACKAGE__->resultset_class( 'DBIx::Class::ResultSet::HashRef' );
20 # in your calling code
21 my $rs = $schema->resultset('User')->search( { } )->hashref_rs;
22 while (my $row = $rs->next) {
26 You can chain up every L<DBIx::Class::ResultSet> method to ->hashref_rs:
28 * ->hashref_rs->all (same as ->hashref_array)
30 * ->hashref_rs->first (same as ->hashref_first)
34 This is a simple way to allow you to set result_class to L<DBIx::Class::ResultClass::HashRefInflator> to
35 skip the fancy objects.
48 Sets result_class to L<DBIx::Class::ResultClass::HashRefInflator> and returns the resultset.
54 $self->result_class('DBIx::Class::ResultClass::HashRefInflator');
58 =head2 hashref_array( )
60 Calls ->hashref_rs->all and returns depending on the calling context an array or an reference to an array.
62 my $rs = $schema->resultset('User')->search( { } )->hashref_array;
65 my @rs = $schema->resultset('User')->search( { } )->hashref_array;
71 return wantarray ? shift->hashref_rs->all : [ shift->hashref_rs->all ];
74 =head2 hashref_first( )
76 Returns the first row of the resultset inflated by L<DBIx::Class::ResultClass::HashRefInflator>.
78 my $first_row = $schema->resultset('User')->search( { } )->hashref_first;
79 print Dumper $first_row
84 return shift->hashref_rs->first;
89 Calls hashref_array and returns a reference to a hash containing the primary key. For each key the corresponding value is a reference to a hash of the resultset inflated by L<DBIx::Class::ResultClass::HashRefInflator>.
91 my $hashref_pk = $schema->resultset('User')->search( { } )->hashref_pk;
92 print Dumper $hashref_pk
98 my @primary_columns = $self->result_source->primary_columns;
99 croak "Multi-column primary keys are not supported." if (scalar @primary_columns > 1 );
100 croak "No primary key found." if (scalar @primary_columns == 0 );
101 my $primary_key = shift @primary_columns;
103 %hash_pk = map { $_->{$primary_key} => $_ } $self->hashref_array;
104 return wantarray ? %hash_pk : \%hash_pk ;
109 Johannes Plunien E<lt>plu@cpan.orgE<gt>
113 Robert Bohne E<lt>rbo@cpan.orgE<gt>
115 =head1 COPYRIGHT AND LICENSE
117 Copyright 2008 by Johannes Plunien
119 This library is free software; you can redistribute it and/or modify
120 it under the same terms as Perl itself.
122 Thanks to mst for his patience.
128 =item * L<DBIx::Class>
130 =item * L<DBIx::Class::ResultClass::HashRefInflator>