package Maypole::Model::CDBI;
use base qw(Maypole::Model::Base Class::DBI);
use Class::DBI::AsForm;
+# use Maypole::Form::CDBI;
+use CGI::Untaint;
+# use Maypole::Form;
+
use Class::DBI::FromCGI;
use Class::DBI::Loader;
use Class::DBI::AbstractSearch;
use Class::DBI::Pager;
use Lingua::EN::Inflect::Number qw(to_PL);
-use CGI::Untaint;
+
use strict;
=head1 NAME
=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.
=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
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};
}
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 {
my $class = shift;
return (
$class->columns("Stringify"),
+ ( grep { /^(name|title)$/i } $class->columns ),
( grep { /(name|title)/i } $class->columns ),
( grep { !/id$/i } $class->primary_columns ),
)[0];
my $oper = "like"; # For now
my %params = %{ $r->{params} };
my %values = map { $_ => { $oper, $params{$_} } }
- grep { defined $params{$_} and $fields{$_} } keys %params;
+ grep { defined $params{$_} && length ($params{$_}) && $fields{$_} }
+ keys %params;
$r->template("list");
if ( !%values ) { return $self->list($r) }
$r->objects(
[
$self->search_where(
- \%values, ( $order ? { order => $order } : () )
+ \%values, ( $order ? { order_by => $order } : () )
)
]
);
sub order {
my ( $self, $r ) = @_;
- my $order;
my %ok_columns = map { $_ => 1 } $self->columns;
- if ( $order = $r->query->{order} and $ok_columns{$order} ) {
- $order .= ( $r->query->{o2} eq "desc" && " DESC" );
- }
- $order;
+ my $q = $r->query;
+ my $order = $q->{order};
+ return unless $order and $ok_columns{$order};
+ $order .= ' DESC' if $q->{o2} and $q->{o2} eq 'desc';
+ return $order;
}
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,
dsn => $dsn,
user => $u,
password => $p,
- options => $opts,
+ %$opts,
)
);
$config->{classes} = [ $config->{loader}->classes ];
$config->{tables} = [ $config->{loader}->tables ];
+ warn( 'Loaded tables: ' . join ',', @{ $config->{tables} } )
+ if $namespace->debug;
}
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;