X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FModel%2FCDBI.pm;h=2c79804642773411585ceca3e564bff1966c048d;hb=688f03716c8809f3e5cd1a2bf95276b3c45747cf;hp=4423b69b63e2b5b2fad74158312b1d86e87da65b;hpb=28823167d12d4cd1419cc6a58900c0fc5819e1af;p=maypole.git diff --git a/lib/Maypole/Model/CDBI.pm b/lib/Maypole/Model/CDBI.pm index 4423b69..2c79804 100644 --- a/lib/Maypole/Model/CDBI.pm +++ b/lib/Maypole/Model/CDBI.pm @@ -16,6 +16,11 @@ It implements a base set of methods required for a Maypole Data Model. It inherits accessor and helper methods from L. +When specified as the application model, it will use Class::DBI::Loader +to generate the model classes from the provided database. If you do not +wish to use this functionality, use L which +will instead use Class::DBI classes provided. + =cut use base qw(Maypole::Model::Base Class::DBI); @@ -38,7 +43,7 @@ use attributes (); Action methods are methods that are accessed through web (or other public) interface. -=item do_edit +=head2 do_edit If there is an object in C<$r-Eobjects>, then it should be edited with the parameters in C<$r-Eparams>; otherwise, a new object should @@ -72,7 +77,25 @@ sub do_edit : Exported { if (%errors) { # Set it up as it was: $r->template_args->{cgi_params} = $r->params; - $r->template_args->{errors} = \%errors; + + # + # replace user unfriendly error messages with something nicer + + foreach (@{$config->{$table}->{required_cols}}) { + next unless ($errors{$_}); + my $key = $_; + s/_/ /g; + $r->template_args->{errors}{ucfirst($_)} = 'This field is required, please provide a valid value'; + $r->template_args->{errors}{$key} = 'This field is required, please provide a valid value'; + delete $errors{$key}; + } + + foreach (keys %errors) { + my $key = $_; + s/_/ /g; + $r->template_args->{errors}{ucfirst($_)} = 'Please provide a valid value for this field'; + $r->template_args->{errors}{$key} = 'Please provide a valid value for this field'; + } undef $obj if $creating; $r->template("edit"); @@ -97,7 +120,9 @@ sub _do_update_or_create { eval { $obj->update_from_cgi( $h => { required => $required_cols, ignore => $ignored_cols, - } ) }; + } ); + $obj->update(); # pos fix for bug 17132 'autoupdate required by do_edit' + }; $fatal = $@; } else { eval { @@ -441,6 +466,9 @@ sub accessor_classes { =head2 stringify_column + Returns the name of the column to use when stringifying + and object. + =cut sub stringify_column { @@ -455,6 +483,13 @@ sub stringify_column { =head2 do_pager + Sets the pager template argument ($r->{template_args}{pager}) + to a Class::DBI::Pager object based on the rows_per_page + value set in the configuration of the application. + + This pager is used via the pager macro in TT Templates, and + is also accessible via Mason. + =cut sub do_pager { @@ -469,6 +504,16 @@ sub do_pager { =head2 order + Returns the SQL order syntax based on the order parameter passed + to the request, and the valid columns.. i.e. 'title ASC' or 'date_created DESC'. + + $sql .= $self->order($r); + + If the order column is not a column of this table, + or an order argument is not passed, then the return value is undefined. + + Note: the returned value does not start with a space. + =cut sub order { @@ -481,8 +526,27 @@ sub order { return $order; } +=head2 setup + + This method is inherited from Maypole::Model::Base and calls setup_database, + which uses Class::DBI::Loader to create and load Class::DBI classes from + the given database schema. + +=cut + =head2 setup_database +The $opts argument is a hashref of options. The "options" key is a hashref of +Database connection options . Other keys may be various Loader arguments or +flags. It has this form: + { + # DB connection options + options { AutoCommit => 1 , ... }, + # Loader args + relationships => 1, + ... + } + =cut sub setup_database { @@ -508,6 +572,12 @@ sub setup_database { if $namespace->debug; } +=head2 class_of + + returns class for given table + +=cut + sub class_of { my ( $self, $r, $table ) = @_; return $r->config->loader->_table2class($table); # why not find_class ?