1 package Apache::MVC::Model::CDBI;
2 use base qw(Apache::MVC::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::AbstractSearch;
13 Apache::MVC::Model::CDBI - Model class based on Class::DBI
17 This is a master model class which uses C<Class::DBI> to do all the hard
18 work of fetching rows and representing them as objects. It is a good
19 model to copy if you're replacing it with other database abstraction
26 # Has-many methods; XXX this is a hack
28 grep { exists $r->{config}{ok_tables}{$_} }
30 keys %{shift->__hasa_list || {}}
33 sub do_edit :Exported {
35 my $h = CGI::Untaint->new(%{$r->{params}});
36 my ($obj) = @{$r->objects};
38 # We have something to edit
39 $obj->update_from_cgi($h);
40 warn "Updating an object ($obj) with ".Dumper($h); use Data::Dumper;
42 $obj = $self->create_from_cgi($h);
44 if (my %errors = $obj->cgi_update_errors) {
45 # Set it up as it was:
46 warn "There were errors: ".Dumper(\%errors)."\n";
47 $r->{template_args}{cgi_params} = $r->{params};
48 $r->{template_args}{errors} = \%errors;
49 $r->{template} = "edit";
51 $r->{template} = "view";
53 $r->objects([ $obj ]);
56 sub delete :Exported {
58 $_->SUPER::delete for @{ $r->objects };
59 $r->objects([ $self->retrieve_all ]);
60 $r->{template} = "list";
64 my ($self, $child) = @_;
65 $child->autoupdate(1);
66 $child->columns( Stringify => qw/ name / );
69 sub search :Exported {
70 return shift->SUPER::search(@_) if caller eq "Class::DBI"; # oops
72 my %fields = map {$_ => 1 } $self->columns;
73 my $oper = "like"; # For now
74 use Carp; Carp::confess("Urgh") unless ref $r;
75 my %params = %{$r->{params}};
76 my %values = map { $_ => {$oper, $params{$_} } }
77 grep { $params{$_} and $fields{$_} } keys %params;
79 $r->objects([ %values ? $self->search_where(%values) : $self->retrieve_all ]);
81 $r->{template_args}{search} = 1;
86 if ( my $rows = $r->config->{rows_per_page}) {
87 $self = $self->pager($rows, $r->query->{page});
88 $r->{template_args}{pager} = $self;
90 $r->objects([ $self->retrieve_all ]);