]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Maypole/Model/CDBI.pm
database_setup fix applied
[maypole.git] / lib / Maypole / Model / CDBI.pm
index 031d9851e797e72497c0ec8681914dcd06766925..8904a425321f9ed4780fcf076426087bc5c79c5c 100644 (file)
@@ -17,7 +17,7 @@ Maypole::Model::CDBI - Model class based on Class::DBI
 
 =head1 DESCRIPTION
 
-This is a master model class which uses C<Class::DBI> to do all the hard
+This is a master model class which uses L<Class::DBI> to do all the hard
 work of fetching rows and representing them as objects. It is a good
 model to copy if you're replacing it with other database abstraction
 modules.
@@ -39,15 +39,17 @@ See L<Maypole::Model::Base> for these:
 
 =item setup_database
 
+=item fetch_objects
+
 =back 
 
-=head1 Additional Commands
+=head1 Additional Actions
 
 =over 
 
 =item delete
 
-Surprisingly, this command causes a database record to be forever lost.
+Unsuprisingly, this command causes a database record to be forever lost.
 
 =item search
 
@@ -80,8 +82,13 @@ sub related {
 
 sub related_class {
     my ( $self, $r, $accessor ) = @_;
-    my $related = $self->related->{$accessor};
-    if ( my $mapping = $related->{args}->{mapping} ) {
+
+    my $related = $self->meta_info( has_many => $accessor ) ||
+                  $self->meta_info( has_a    => $accessor ) ||
+                  return;
+
+    my $mapping = $related->{args}->{mapping};
+    if ( @$mapping ) {
         return $related->{foreign_class}->meta_info('has_a')->{ $$mapping[0] }
           ->{foreign_class};
     }
@@ -95,32 +102,41 @@ sub do_edit : Exported {
     my $h        = CGI::Untaint->new( %{ $r->{params} } );
     my $creating = 0;
     my ($obj) = @{ $r->objects || [] };
+    my $fatal;
     if ($obj) {
-
         # We have something to edit
-        $obj->update_from_cgi( $h =>
-              { required => $r->{config}{ $r->{table} }{required_cols} || [], }
-        );
+        eval {
+            $obj->update_from_cgi( $h =>
+                { required => $r->{config}{ $r->{table} }{required_cols} || [], }
+            );
+        };
+        $fatal = $@;
     }
     else {
-        $obj =
-          $self->create_from_cgi( $h =>
-              { required => $r->{config}{ $r->{table} }{required_cols} || [], }
-          );
+        eval {
+            $obj =
+                $self->create_from_cgi( $h =>
+                    { required => $r->{config}{ $r->{table} }{required_cols} || [], }
+            );
+        };
+        if ($fatal = $@) {
+            warn "$fatal" if $r->debug;
+        }
         $creating++;
     }
-    if ( my %errors = $obj->cgi_update_errors ) {
+    if ( my %errors = $fatal ? (FATAL => $fatal) : $obj->cgi_update_errors ) {
 
         # Set it up as it was:
         $r->{template_args}{cgi_params} = $r->{params};
         $r->{template_args}{errors}     = \%errors;
-        $r->{template}                  = "edit";
-        undef $obj if $creating;    # Couldn't create
+
+        undef $obj if $creating;
+        $r->template("edit");
     }
     else {
         $r->{template} = "view";
     }
-    $r->objects( [$obj] );
+    $r->objects( $obj ? [$obj] : []);
 }
 
 sub delete : Exported {
@@ -158,7 +174,8 @@ sub search : Exported {
     my $oper   = "like";                                # For now
     my %params = %{ $r->{params} };
     my %values = map { $_ => { $oper, $params{$_} } }
-      grep { length ($params{$_}) and $fields{$_} } keys %params;
+      grep { defined $params{$_} && length ($params{$_}) && $fields{$_} }
+      keys %params;
 
     $r->template("list");
     if ( !%values ) { return $self->list($r) }
@@ -219,7 +236,7 @@ sub setup_database {
             dsn       => $dsn,
             user      => $u,
             password  => $p,
-            options   => $opts,
+           %$opts,
         )
     );
     $config->{classes} = [ $config->{loader}->classes ];
@@ -233,4 +250,15 @@ sub class_of {
     return $r->config->loader->_table2class($table);
 }
 
+sub fetch_objects {
+    my ($class, $r)=@_;
+    my @pcs = $class->primary_columns;
+    if ( $#pcs ) {
+    my %pks;
+        @pks{@pcs}=(@{$r->{args}});
+        return $class->retrieve( %pks );
+    }
+    return $class->retrieve( $r->{args}->[0] );
+}
+
 1;