1 package Maypole::Model::CDBI;
2 use base qw(Maypole::Model::Base Class::DBI);
3 use Lingua::EN::Inflect::Number qw(to_PL);
4 use Class::DBI::AsForm;
5 use Class::DBI::FromCGI;
6 use Class::DBI::Loader;
7 use Class::DBI::AbstractSearch;
8 use Class::DBI::Plugin::RetrieveAll;
15 Maypole::Model::CDBI - Model class based on Class::DBI
19 This is a master model class which uses C<Class::DBI> to do all the hard
20 work of fetching rows and representing them as objects. It is a good
21 model to copy if you're replacing it with other database abstraction
28 # Has-many methods; XXX this is a hack
30 grep { exists $r->{config}{ok_tables}{$_} }
32 keys %{shift->__hasa_list || {}}
35 sub do_edit :Exported {
37 my $h = CGI::Untaint->new(%{$r->{params}});
38 my ($obj) = @{$r->objects || []};
40 # We have something to edit
41 $obj->update_from_cgi($h);
42 warn "Updating an object ($obj) with ".Dumper($h); use Data::Dumper;
44 $obj = $self->create_from_cgi($h);
46 if (my %errors = $obj->cgi_update_errors) {
47 # Set it up as it was:
48 warn "There were errors: ".Dumper(\%errors)."\n";
49 $r->{template_args}{cgi_params} = $r->{params};
50 $r->{template_args}{errors} = \%errors;
51 $r->{template} = "edit";
53 $r->{template} = "view";
55 $r->objects([ $obj ]);
58 sub delete :Exported {
59 return shift->SUPER::delete(@_) if caller ne "Maypole::Model::Base";
61 $_->SUPER::delete for @{ $r->objects || [] };
62 $r->objects([ $self->retrieve_all ]);
63 $r->{template} = "list";
67 my ($self, $child) = @_;
68 $child->autoupdate(1);
69 if (grep { $_ eq "name" } $child->columns) { # Common case
70 $child->columns( Stringify => qw/ name / );
71 } # Otherwise, work it out for yourself.
74 sub search :Exported {
75 return shift->SUPER::search(@_) if caller ne "Maypole::Model::Base";
78 my %fields = map {$_ => 1 } $self->columns;
79 my $oper = "like"; # For now
80 use Carp; Carp::confess("Urgh") unless ref $r;
81 my %params = %{$r->{params}};
82 my %values = map { $_ => {$oper, $params{$_} } }
83 grep { $params{$_} and $fields{$_} } keys %params;
85 $self = $self->do_pager($r);
86 $r->objects([ %values ? $self->search_where(%values) : $self->retrieve_all ]);
88 $r->{template_args}{search} = 1;
93 if ( my $rows = $r->config->{rows_per_page}) {
94 return $r->{template_args}{pager} = $self->pager($rows, $r->query->{page});
95 } else { return $self }
100 my %ok_columns = map {$_ => 1} $self->columns;
101 $self = $self->do_pager($r);
103 if ($order = $r->query->{order} and $ok_columns{$order}) {
104 $r->objects([ $self->retrieve_all_sorted_by( $order.
105 ($r->query->{o2} eq "desc" && " DESC")
108 $r->objects([ $self->retrieve_all ]);
113 my ($self, $config, $namespace, $dsn, $u, $p) = @_;
114 $config->{dsn} = $dsn;
115 $config->{loader} = Class::DBI::Loader->new(
116 namespace => $namespace,
121 $config->{classes} = [ $config->{loader}->classes ];
122 $config->{tables} = [ $config->{loader}->tables ];
126 my ($self, $r, $table) = @_;
127 return $r->config->{loader}->_table2class($table);