X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FModel%2FCDBI.pm;h=4ed0aa68b331179ace9c43f7ec516c9338a2759a;hb=d2c852208417c014caec0436a677fcadccc197d8;hp=54325616f90cac9dfcfaa5d3ff3474639a3cf0b1;hpb=ef3da5818c452a331e863d30efc41f5b65d0f624;p=maypole.git diff --git a/lib/Maypole/Model/CDBI.pm b/lib/Maypole/Model/CDBI.pm index 5432561..4ed0aa6 100644 --- a/lib/Maypole/Model/CDBI.pm +++ b/lib/Maypole/Model/CDBI.pm @@ -17,7 +17,7 @@ Maypole::Model::CDBI - Model class based on Class::DBI =head1 DESCRIPTION -This is a master model class which uses C to do all the hard +This is a master model class which uses L 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,26 +39,40 @@ See L 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 +The search action + +=back + =head1 Helper Methods +=over + =item order =item stringify_column =item do_pager +=item related_class + +Given an accessor name as a method, this function returns the class this accessor returns. + +=back + =cut sub related { @@ -66,42 +80,56 @@ sub related { return keys %{ $self->meta_info('has_many') || {} }; } -#sub related_class { -# my ( $self, $r, $accessor ) = @_; -# my $related=$self->related->{$accessor}; -# if ( my $mapping=$related->{args}->{mapping} ) { -# return $related->{foreign_class}->meta_info('has_a')->{$$mapping[0]}->{foreign_class}; -# } else { -# return $related->{foreign_class}; -# } -#} +sub related_class { + my ( $self, $r, $accessor ) = @_; + + 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}; + } + else { + return $related->{foreign_class}; + } +} sub do_edit : Exported { my ( $self, $r ) = @_; 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 + + undef $obj if $creating; + $r->template("edit"); } else { $r->{template} = "view"; @@ -144,7 +172,8 @@ sub search : Exported { my $oper = "like"; # For now my %params = %{ $r->{params} }; my %values = map { $_ => { $oper, $params{$_} } } - grep { $params{$_} and $fields{$_} } keys %params; + grep { defined $params{$_} && length ($params{$_}) && $fields{$_} } + keys %params; $r->template("list"); if ( !%values ) { return $self->list($r) } @@ -153,7 +182,7 @@ sub search : Exported { $r->objects( [ $self->search_where( - \%values, ( $order ? { order => $order } : () ) + \%values, ( $order ? { order_by => $order } : () ) ) ] ); @@ -192,12 +221,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, @@ -209,6 +239,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 { @@ -216,5 +248,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;