my $first_row = $schema->resultset('User')->search( { } )->hashref_first;
print Dumper $first_row
+ hashref_pk( )
+ 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
+ DBIx::Class::ResultClass::HashRefInflator.
+
+ my $hashref_pk = $schema->resultset('User')->search( { } )->hashref_pk;
+ print Dumper $hashref_pk
+
AUTHOR
Johannes Plunien <plu@cpan.org>
use warnings;
use strict;
+use Carp;
use base qw( DBIx::Class::ResultSet );
use DBIx::Class::ResultClass::HashRefInflator;
return shift->hashref_rs->first;
}
+=head2 hashref_pk( )
+
+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>.
+
+ my $hashref_pk = $schema->resultset('User')->search( { } )->hashref_pk;
+ print Dumper $hashref_pk
+
+=cut
+
+sub hashref_pk{
+ my $self = shift;
+ my @primary_columns = $self->result_source->primary_columns;
+ croak "Multi-column primary keys are not supported." if (scalar @primary_columns > 1 );
+ croak "No primary key found." if (scalar @primary_columns == 0 );
+ my $primary_key = shift @primary_columns;
+ my %hash_pk = ();
+ %hash_pk = map { $_->{$primary_key} => $_ } $self->hashref_array;
+ return wantarray ? %hash_pk : \%hash_pk ;
+}
+
=head1 AUTHOR
Johannes Plunien E<lt>plu@cpan.orgE<gt>
eval "use SQL::Translator ();";
plan skip_all => 'SQL::Translator required to run this test' if $@;
- plan( tests => 10 );
+ plan( tests => 11 );
}
use lib 't/lib';
"hashref_first"
);
}
+
+{
+ my $hashref = $schema->resultset('User')->search( {}, { order_by => 'me.id ASC' } )->hashref_pk;
+ is_deeply(
+ $hashref,
+ {
+ 1 => {
+ 'id' => '1',
+ 'login' => 'root'
+ },
+ 2 => {
+ 'id' => '2',
+ 'login' => 'toor'
+ },
+ 3 => {
+ 'id' => '3',
+ 'login' => 'daemon'
+ },
+ 4 => {
+ 'id' => '4',
+ 'login' => 'operator'
+ },
+ 5 => {
+ 'id' => '5',
+ 'login' => 'bin'
+ },
+ 6 => {
+ 'id' => '6',
+ 'login' => 'tty'
+ }
+ },
+ 'hashref_pk'
+ );
+}