1CGI::Application::PlugiUns:e:rDBCIoxn:t:rCilbaustse(d3C)PGeIr:l:ADpopcluimceanttiaotni:o:nPlugin::DBIx::Class(3)
2
3
4

NAME

6       CGI::Application::Plugin::DBIx::Class - Access a DBIx::Class Schema
7       from a CGI::Application
8

VERSION

10       version 1.000101
11

SYNOPSIS

13        use CGI::Application::Plugin::DBIx::Class ':all';
14
15        sub cgiapp_init  {
16            my $self = shift;
17
18            $self->dbic_config({
19               schema => 'MyApp::Schema',
20               connect_info => {
21                  dsn => $data_source,
22                  user => $username,
23                  password => $password,
24               },
25            });
26        }
27
28        sub person {
29           my $self   = shift;
30           my $id     = $self->query->param('id');
31           my $person = $self->schema->resultset('People')->find($id);
32           # ...
33        }
34
35        sub people {
36           my $self   = shift;
37           my $people = $self->page_and_sort(
38               $self->simple_search(
39                  $self->schema->resultset('People')
40               )
41           );
42           # ...
43        }
44

DESCRIPTION

46       This module helps you to map various DBIx::Class features to CGI
47       parameters.  For the most part that means it will help you search,
48       sort, and paginate with a minimum of effort and thought.
49

METHODS

51   dbic_config
52        $self->dbic_config({
53          schema => MyApp::Schema->connect(@connection_data),
54          connect_info => { ... },
55        });
56
57       You must run this method in setup or cgiapp_init to setup your schema.
58
59       Valid arguments are:
60
61        schema - Required, Name of DBIC Schema
62        connect_info - Optional, these arguments are what are passed to connect, if
63          this isn't passed and a C<dbh> method exists, that will be used
64        ignored_params - Optional, Params to ignore when doing a simple search or sort,
65           defaults to
66        [qw{limit start sort dir _dc rm xaction}]
67
68        page_size - Optional, amount of results per page, defaults to 25
69
70   page_and_sort
71        my $resultset = $self->schema->resultset('Foo');
72        my $result = $self->page_and_sort($resultset);
73
74       This is a helper method that will first sort (with "simple_sort") your
75       data and then paginate it.  Returns a resultset.
76
77   paginate
78        my $resultset = $self->schema->resultset('Foo');
79        my $result = $self->paginate($resultset);
80
81       Paginates the passed in resultset based on the following CGI
82       parameters:
83
84        start - first row to display
85        limit - amount of rows per page
86
87       Returns a resultset.
88
89   schema
90        my $schema = $self->schema;
91
92       This is just a basic accessor method for your schema
93
94   search
95        my $resultset   = $self->schema->resultset('Foo');
96        my $searched_rs = $self->search($resultset);
97
98       Calls the controller_search method on the passed in resultset with all
99       of the CGI parameters.  I like to have this look something like the
100       following:
101
102        # Base search dispatcher, defined in MyApp::Schema::ResultSet
103        sub _build_search {
104           my $self           = shift;
105           my $dispatch_table = shift;
106           my $q              = shift;
107
108           my %search = ();
109           my %meta   = ();
110
111           foreach ( keys %{$q} ) {
112              if ( my $fn = $dispatch_table->{$_} and $q->{$_} ) {
113                 my ( $tmp_search, $tmp_meta ) = $fn->( $q->{$_} );
114                 %search = ( %search, %{$tmp_search} );
115                 %meta   = ( %meta,   %{$tmp_meta} );
116              }
117           }
118
119           return $self->search(\%search, \%meta);
120        }
121
122        # search method in specific resultset
123        sub controller_search {
124           my $self   = shift;
125           my $params = shift;
126           return $self->_build_search({
127                 status => sub {
128                    return { 'repair_order_status' => shift }, {};
129                 },
130                 part_id => sub {
131                    return {
132                       'lineitems.part_id' => { -like => q{%}.shift( @_ ).q{%} }
133                    }, { join => 'lineitems' };
134                 },
135                 serial => sub {
136                    return {
137                       'lineitems.serial' => { -like => q{%}.shift( @_ ).q{%} }
138                    }, { join => 'lineitems' };
139                 },
140                 id => sub {
141                    return { 'id' => shift }, {};
142                 },
143                 customer_id => sub {
144                    return { 'customer_id' => shift }, {};
145                 },
146                 repair_order_id => sub {
147                    return {
148                       'repair_order_id' => { -like => q{%}.shift( @_ ).q{%} }
149                    }, {};
150                 },
151              },$params
152           );
153        }
154
155   sort
156        my $resultset = $self->schema->resultset('Foo');
157        my $result = $self->sort($resultset);
158
159       Exactly the same as search, except calls controller_sort.  Here is how
160       I use it:
161
162        # Base sort dispatcher, defined in MyApp::Schema::ResultSet
163        sub _build_sort {
164           my $self = shift;
165           my $dispatch_table = shift;
166           my $default = shift;
167           my $q = shift;
168
169           my %search = ();
170           my %meta   = ();
171
172           my $direction = $q->{dir};
173           my $sort      = $q->{sort};
174
175           if ( my $fn = $dispatch_table->{$sort} ) {
176              my ( $tmp_search, $tmp_meta ) = $fn->( $direction );
177              %search = ( %search, %{$tmp_search} );
178              %meta   = ( %meta,   %{$tmp_meta} );
179           } elsif ( $sort && $direction ) {
180              my ( $tmp_search, $tmp_meta ) = $default->( $sort, $direction );
181              %search = ( %search, %{$tmp_search} );
182              %meta   = ( %meta,   %{$tmp_meta} );
183           }
184
185           return $self->search(\%search, \%meta);
186        }
187
188        # sort method in specific resultset
189        sub controller_sort {
190           my $self = shift;
191           my $params = shift;
192           return $self->_build_sort({
193                first_name => sub {
194                   my $direction = shift;
195                   return {}, {
196                      order_by => { "-$direction" => [qw{last_name first_name}] },
197                   };
198                },
199              }, sub {
200             my $param = shift;
201             my $direction = shift;
202             return {}, {
203                order_by => { "-$direction" => $param },
204             };
205              },$params
206           );
207        }
208
209   simple_deletion
210        $self->simple_deletion({ rs => 'Foo' });
211
212       Deletes from the passed in resultset based on the following CGI
213       parameter:
214
215        to_delete - values of the ids of items to delete
216
217       Valid arguments are:
218
219        rs - resultset loaded into schema
220
221       Note that this method uses the $rs->delete method, as opposed to
222       $rs->delete_all
223
224   simple_search
225        my $searched_rs = $self->simple_search({ rs => 'Foo' });
226
227       This method just searches on all of the CGI parameters that are not in
228       the "ignored_params" with a like "%$value%".  If there are multiple
229       values it will make the search an "or" between the different values.
230
231       Valid arguments are:
232
233        rs - source loaded into schema
234
235   simple_sort
236        my $resultset = $self->schema->resultset('Foo');
237        my $sorted_rs = $self->simple_sort($resultset);
238
239       Sorts the passed in resultset based on the following CGI parameters:
240
241        sort - field to sort by, defaults to primarky key
242        dir  - direction to sort
243

SEE ALSO

245       CGI::Application::Plugin::DBH
246

CREDITS

248       Thanks to Micro Technology Services, Inc. for funding the initial
249       development of this module.
250

AUTHOR

252       Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
253
255       This software is copyright (c) 2012 by Arthur Axel "fREW" Schmidt.
256
257       This is free software; you can redistribute it and/or modify it under
258       the same terms as the Perl 5 programming language system itself.
259
260
261
262perl v5.32.0                      2020-C0G7I-:2:8Application::Plugin::DBIx::Class(3)
Impressum