X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FModel%2FCDBI.pm;h=37b133549ab316dfee9b246ab8124f2eea35e9d0;hb=f76351a09c6594711ebcae3ab07f9dde9c04367e;hp=f409fff2a0404f7d2dc56369cd04dcbdd23c40d4;hpb=7dda5e1451220612c6ad749746d84e4c090e330c;p=maypole.git diff --git a/lib/Maypole/Model/CDBI.pm b/lib/Maypole/Model/CDBI.pm index f409fff..37b1335 100644 --- a/lib/Maypole/Model/CDBI.pm +++ b/lib/Maypole/Model/CDBI.pm @@ -39,6 +39,8 @@ See L for these: =item setup_database +=item fetch_objects + =back =head1 Additional Commands @@ -95,27 +97,38 @@ 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} || [], } + ); + }; + $fatal = $@; $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 + + if ($creating) { + undef $obj; + $r->template("addnew"); + } else { + $r->template("edit"); + } } else { $r->{template} = "view"; @@ -158,7 +171,7 @@ sub search : Exported { my $oper = "like"; # For now my %params = %{ $r->{params} }; my %values = map { $_ => { $oper, $params{$_} } } - grep { defined $params{$_} and $fields{$_} } keys %params; + grep { length ($params{$_}) and $fields{$_} } keys %params; $r->template("list"); if ( !%values ) { return $self->list($r) } @@ -167,7 +180,7 @@ sub search : Exported { $r->objects( [ $self->search_where( - \%values, ( $order ? { order => $order } : () ) + \%values, ( $order ? { order_by => $order } : () ) ) ] ); @@ -206,12 +219,13 @@ sub list : Exported { } sub setup_database { - my ( $self, $config, $namespace, $dsn, $u, $p, $opts ) = @_; + my ( $class, $config, $namespace, $dsn, $u, $p, $opts ) = @_; $dsn ||= $config->dsn; $u ||= $config->user; $p ||= $config->pass; $opts ||= $config->opts; $config->dsn($dsn); + warn "No DSN set in config" unless $dsn; $config->loader || $config->loader( Class::DBI::Loader->new( namespace => $namespace, @@ -223,6 +237,8 @@ sub setup_database { ); $config->{classes} = [ $config->{loader}->classes ]; $config->{tables} = [ $config->{loader}->tables ]; + warn( 'Loaded tables: ' . join ',', @{ $config->{tables} } ) + if $namespace->debug; } sub class_of { @@ -230,5 +246,15 @@ sub class_of { return $r->config->loader->_table2class($table); } -1; +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;