1 package Maypole::Model::CDBI;
2 use base qw(Maypole::Model::Base Class::DBI);
3 use Class::DBI::AsForm;
4 # use Maypole::Form::CDBI;
8 use Class::DBI::FromCGI;
9 use Class::DBI::Loader;
10 use Class::DBI::AbstractSearch;
11 use Class::DBI::Plugin::RetrieveAll;
12 use Class::DBI::Pager;
14 use Lingua::EN::Inflect::Number qw(to_PL);
20 Maypole::Model::CDBI - Model class based on Class::DBI
24 This is a master model class which uses L<Class::DBI> to do all the hard
25 work of fetching rows and representing them as objects. It is a good
26 model to copy if you're replacing it with other database abstraction
29 It implements a base set of methods required for a Maypole Data Model.
30 See L<Maypole::Model::Base> for these:
50 =head1 Additional Actions
56 Unsuprisingly, this command causes a database record to be forever lost.
70 =item stringify_column
76 Given an accessor name as a method, this function returns the class this accessor returns.
83 my ( $self, $r ) = @_;
84 return keys %{ $self->meta_info('has_many') || {} };
88 my ( $self, $r, $accessor ) = @_;
89 my $meta = $self->meta_info;
90 my @rels = keys %$meta;
93 $related = $meta->{$_}{$accessor};
96 return unless $related;
98 my $mapping = $related->{args}->{mapping};
99 if ( $mapping and @$mapping ) {
100 return $related->{foreign_class}->meta_info('has_a')->{$$mapping[0]}->{foreign_class};
103 return $related->{foreign_class};
108 sub do_edit : Exported
110 my ($self, $r, $obj) = @_;
112 my $config = $r->config;
113 my $table = $r->table;
115 my $required_cols = $config->{$table}->{required_cols} || [];
116 my $ignored_cols = $r->{config}{ $r->{table} }{ignore_cols};
118 ($obj, my $fatal, my $creating) = $self->_do_update_or_create($r, $obj, $required_cols, $ignored_cols);
120 # handle errors, if none, proceed to view the newly created/updated object
121 my %errors = $fatal ? (FATAL => $fatal) : $obj->cgi_update_errors;
125 # Set it up as it was:
126 $r->template_args->{cgi_params} = $r->params;
127 $r->template_args->{errors} = \%errors;
129 undef $obj if $creating;
130 $r->template("edit");
134 $r->template("view");
137 $r->objects( $obj ? [$obj] : []);
140 # drb - I've (probably temporarily) split this out from do_edit, so it's
141 # reported by Mp::P::Trace
142 sub _do_update_or_create
144 my ($self, $r, $obj, $required_cols, $ignored_cols) = @_;
148 my $h = CGI::Untaint->new( %{$r->params} );
153 # We have something to edit
154 eval { $obj->update_from_cgi( $h => {
155 required => $required_cols,
156 ignore => $ignored_cols,
163 $obj = $self->create_from_cgi( $h => {
164 required => $required_cols,
165 ignore => $ignored_cols,
171 warn "$fatal" if $r->debug;
176 return $obj, $fatal, $creating;
179 sub delete : Exported {
181 my ($sub) = (caller(1))[3];
182 $sub =~ /^(.+)::([^:]+)$/;
183 # So subclasses can still send search down ...
184 return ($1 ne "Maypole::Model::Base" && $2 ne "delete") ?
185 $self->SUPER::search(@_) : $self->do_delete(@_);
189 my ( $self, $r ) = @_;
190 $_->SUPER::delete for @{ $r->objects || [] };
191 $r->objects( [ $self->retrieve_all ] );
192 $r->{template} = "list";
196 sub stringify_column {
199 $class->columns("Stringify"),
200 ( grep { /^(name|title)$/i } $class->columns ),
201 ( grep { /(name|title)/i } $class->columns ),
202 ( grep { !/id$/i } $class->primary_columns ),
207 my ( $self, $child ) = @_;
208 $child->autoupdate(1);
209 if ( my $col = $child->stringify_column ) {
210 $child->columns( Stringify => $col );
214 sub search : Exported {
216 my ($sub) = (caller(1))[3];
217 $sub =~ /^(.+)::([^:]+)$/;
218 # So subclasses can still send search down ...
219 return ($1 ne "Maypole::Model::Base" && $2 ne "search") ?
220 $self->SUPER::search(@_) : $self->do_search(@_);
223 sub do_search : Exported {
224 my ( $self, $r ) = @_;
225 my %fields = map { $_ => 1 } $self->columns;
226 my $oper = "like"; # For now
227 my %params = %{ $r->{params} };
228 my %values = map { $_ => { $oper, $params{$_} } }
229 grep { defined $params{$_} && length ($params{$_}) && $fields{$_} }
232 $r->template("list");
233 if ( !%values ) { return $self->list($r) }
234 my $order = $self->order($r);
235 $self = $self->do_pager($r);
239 \%values, ( $order ? { order_by => $order } : () )
243 $r->{template_args}{search} = 1;
247 my ( $self, $r ) = @_;
248 if ( my $rows = $r->config->rows_per_page ) {
249 return $r->{template_args}{pager} =
250 $self->pager( $rows, $r->query->{page} );
252 else { return $self }
256 my ( $self, $r ) = @_;
257 my %ok_columns = map { $_ => 1 } $self->columns;
259 my $order = $q->{order};
260 return unless $order and $ok_columns{$order};
261 $order .= ' DESC' if $q->{o2} and $q->{o2} eq 'desc';
265 sub list : Exported {
266 my ( $self, $r ) = @_;
267 my $order = $self->order($r);
268 $self = $self->do_pager($r);
270 $r->objects( [ $self->retrieve_all_sorted_by($order) ] );
273 $r->objects( [ $self->retrieve_all ] );
278 my ( $class, $config, $namespace, $dsn, $u, $p, $opts ) = @_;
279 $dsn ||= $config->dsn;
280 $u ||= $config->user;
281 $p ||= $config->pass;
282 $opts ||= $config->opts;
284 warn "No DSN set in config" unless $dsn;
285 $config->loader || $config->loader(
286 Class::DBI::Loader->new(
287 namespace => $namespace,
294 $config->{classes} = [ $config->{loader}->classes ];
295 $config->{tables} = [ $config->{loader}->tables ];
296 warn( 'Loaded tables: ' . join ',', @{ $config->{tables} } )
297 if $namespace->debug;
301 my ( $self, $r, $table ) = @_;
302 return $r->config->loader->_table2class($table); # why not find_class ?
307 my @pcs = $class->primary_columns;
310 @pks{@pcs}=(@{$r->{args}});
311 return $class->retrieve( %pks );
313 return $class->retrieve( $r->{args}->[0] );