CGI::Ap...x::Class(3)
β¨ NAME β¨
okay so bestie, this page is CGI::Ap...x::Class(3) β straight out of the User Contributed Perl Documentation, and it's about CGI::Application::Plugin::DBIx::Class, which is the lil guy that gives you access to a DBIx::Class schema from a CGI::Application. it's giving glue code, it's giving convenience, and honestly? we love her. π¦
π VERSION π
version 1.000101 β¨
πΈ SYNOPSIS πΈ
use CGI::Application::Plugin::DBIx::Class ':all';
sub cgiapp_init {
my $self = shift;
$self->dbic_config({
schema => 'MyApp::Schema',
connect_info => {
dsn => $data_source,
user => $username,
password => $password,
},
});
}
sub person {
my $self = shift;
my $id = $self->query->param('id');
my $person = $self->schema->resultset('People')->find($id);
# ...
}
sub people {
my $self = shift;
my $people = $self->page_and_sort(
$self->simple_search(
$self->schema->resultset('People')
)
);
# ...
}
π« DESCRIPTION π«
This module helps you map all the various DBIx::Class features onto CGI parameters. lowkey that's the entire plot β and for the most part it means it'll help you search, sort, and paginate with a minimum of effort and thought. no thoughts, head empty, resultset returned. π«Ά
π METHODS π
π dbic_config
$self->dbic_config({
schema => MyApp::Schema->connect(@connection_data),
connect_info => { ... },
});
You must run this method in setup or cgiapp_init to set up your schema. like, that's not optional, bestie. π¨
Valid arguments are:
schemaβ Required. Name of DBIC Schema.connect_infoβ Optional. These arguments are what get passed toconnect. If you don't pass this and adbhmethod exists, then that one gets used instead.ignored_paramsβ Optional. Params to ignore when doing a simple search or sort. Defaults to[qw{limit start sort dir _dc rm xaction}].page_sizeβ Optional. Amount of results per page. Defaults to25.
π¦ page_and_sort
my $resultset = $self->schema->resultset('Foo');
my $result = $self->page_and_sort($resultset);
This is a helper method that will first sort your data (with "simple_sort") and then paginate it. Returns a resultset. two-step glow-up, one call. β¨
π paginate
my $resultset = $self->schema->resultset('Foo');
my $result = $self->paginate($resultset);
Paginates the resultset you hand it, based on these CGI parameters:
startβ first row to displaylimitβ amount of rows per page
Returns a resultset.
π schema
my $schema = $self->schema;
This is just a basic accessor method for your schema. simple, iconic, no drama. π
π₯ search
my $resultset = $self->schema->resultset('Foo');
my $searched_rs = $self->search($resultset);
Calls the controller_search method on the resultset you passed in, with all of the CGI parameters. I like to have this look something like the following:
# Base search dispatcher, defined in MyApp::Schema::ResultSet
sub _build_search {
my $self = shift;
my $dispatch_table = shift;
my $q = shift;
my %search = ();
my %meta = ();
foreach ( keys %{$q} ) {
if ( my $fn = $dispatch_table->{$_} and $q->{$_} ) {
my ( $tmp_search, $tmp_meta ) = $fn->( $q->{$_} );
%search = ( %search, %{$tmp_search} );
%meta = ( %meta, %{$tmp_meta} );
}
}
return $self->search(\%search, \%meta);
}
# search method in specific resultset
sub controller_search {
my $self = shift;
my $params = shift;
return $self->_build_search({
status => sub {
return { 'repair_order_status' => shift }, {};
},
part_id => sub {
return {
'lineitems.part_id' => { -like => q{%}.shift( @_ ).q{%} }
}, { join => 'lineitems' };
},
serial => sub {
return {
'lineitems.serial' => { -like => q{%}.shift( @_ ).q{%} }
}, { join => 'lineitems' };
},
id => sub {
return { 'id' => shift }, {};
},
customer_id => sub {
return { 'customer_id' => shift }, {};
},
repair_order_id => sub {
return {
'repair_order_id' => { -like => q{%}.shift( @_ ).q{%} }
}, {};
},
},$params
);
}
π sort
my $resultset = $self->schema->resultset('Foo');
my $result = $self->sort($resultset);
Exactly the same as search, except it calls controller_sort. Here is how I use it:
# Base sort dispatcher, defined in MyApp::Schema::ResultSet
sub _build_sort {
my $self = shift;
my $dispatch_table = shift;
my $default = shift;
my $q = shift;
my %search = ();
my %meta = ();
my $direction = $q->{dir};
my $sort = $q->{sort};
if ( my $fn = $dispatch_table->{$sort} ) {
my ( $tmp_search, $tmp_meta ) = $fn->( $direction );
%search = ( %search, %{$tmp_search} );
%meta = ( %meta, %{$tmp_meta} );
} elsif ( $sort && $direction ) {
my ( $tmp_search, $tmp_meta ) = $default->( $sort, $direction );
%search = ( %search, %{$tmp_search} );
%meta = ( %meta, %{$tmp_meta} );
}
return $self->search(\%search, \%meta);
}
# sort method in specific resultset
sub controller_sort {
my $self = shift;
my $params = shift;
return $self->_build_sort({
first_name => sub {
my $direction = shift;
return {}, {
order_by => { "-$direction" => [qw{last_name first_name}] },
};
},
}, sub {
my $param = shift;
my $direction = shift;
return {}, {
order_by => { "-$direction" => $param },
};
},$params
);
}
π simple_deletion
$self->simple_deletion({ rs => 'Foo' });
Deletes from the resultset you passed in, based on this CGI parameter:
to_deleteβ values of the ids of items to delete
Valid arguments are:
rsβ resultset loaded into schema
β οΈ heads up bestie: this method uses the$rs->deletemethod, as opposed to$rs->delete_all. the difference is real and you should know which one you're summoning. π
π§ simple_search
my $searched_rs = $self->simple_search({ rs => 'Foo' });
This method just searches on all of the CGI parameters that are not in the "ignored_params", with a like "%$value%". If there are multiple values, it makes the search an "or" between the different values. flexible queen. πΈ
Valid arguments are:
rsβ source loaded into schema
β simple_sort
my $resultset = $self->schema->resultset('Foo');
my $sorted_rs = $self->simple_sort($resultset);
Sorts the resultset you passed in, based on these CGI parameters:
sortβ field to sort by, defaults to primarky keydirβ direction to sort
(and yes, "primarky" is spelled exactly like that in the source. we don't fix, we only report. π )
π SEE ALSO π
CGI::Application::Plugin::DBH
π CREDITS π
Thanks to Micro Technology Services, Inc. for funding the initial development of this module. funding the lore, honestly. π«Ά
βοΈ AUTHOR βοΈ
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2012 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page bestie π this was CGI::Ap...x::Class(3) as generated by perl v5.42.0, dated 2025-07-25 β go forth and paginate responsibly. mwah. πβ¨
π¦ where this page comes from
- fedora 43: perl-CGI-Application-Plugin-DBIx-Class 1.000101-35.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms