CGI::Ap...x::Class(3)

πŸ“¦
version 1.000101 Β· 2 versions
, in fedora 43

✨ 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 to connect. If you don't pass this and a dbh method 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 to 25.

πŸ¦‹ 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 display
  • limit β€” 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. πŸ’…

 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->delete method, as opposed to $rs->delete_all. the difference is real and you should know which one you're summoning. 😭
 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 key
  • dir β€” 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>


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