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 0.100210
11

SYNOPSIS

13        use CGI::Application::Plugin::DBH (qw/dbh_config dbh/);
14        use CGI::Application::Plugin::DBIx::Class ':all';
15
16        sub cgiapp_init  {
17            my $self = shift;
18
19            $self->dbh_config($data_source, $username, $auth, \%attr);
20            $self->dbic_config({schema => 'MyApp::Schema' });
21        }
22
23        sub person {
24           my $self   = shift;
25           my $id     = $self->query->param('id');
26           my $person = $self->schema->resultset('People')->find($id);
27           # ...
28        }
29
30        sub people {
31           my $self   = shift;
32           my $people = $self->page_and_sort(
33               $self->simple_search(
34                  $self->schema->resultset('People')
35               )
36           );
37           # ...
38        }
39

DESCRIPTION

41       This module helps you to map various DBIx::Class features to CGI
42       parameters.  For the most part that means it will help you search,
43       sort, and paginate with a minimum of effort and thought.  Currently it
44       uses the connection from CGI::Application::Plugin::DBH.
45

METHODS

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

SEE ALSO

236       CGI::Application::Plugin::DBH
237

CREDITS

239       Thanks to Micro Technology Services, Inc. for funding the initial
240       development of this module.
241

AUTHOR

243         Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
244
246       This software is copyright (c) 2010 by Arthur Axel "fREW" Schmidt.
247
248       This is free software; you can redistribute it and/or modify it under
249       the same terms as the Perl 5 programming language system itself.
250
251
252
253perl v5.12.0                      2010-C0G1I-:2:2Application::Plugin::DBIx::Class(3)
Impressum