]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Apache/MVC/Model/CDBI.pm
And now we have paging support. (And some better docs)
[maypole.git] / lib / Apache / MVC / Model / CDBI.pm
index 4d6339d1745b0201d4e9c6655051b79f26fe751e..854118e1559f2007fda5983a31a41b2708a604ae 100644 (file)
@@ -4,17 +4,22 @@ use Lingua::EN::Inflect::Number qw(to_PL);
 use Class::DBI::AsForm;
 use Class::DBI::FromCGI;
 use Class::DBI::AbstractSearch;
+use Class::DBI::Pager;
 use CGI::Untaint;
 use strict;
 
-sub description { "A poorly defined class" }
+=head1 NAME
 
-sub column_names { my $class = shift; map { $_ => ucfirst $_ } $class->columns }
+Apache::MVC::Model::CDBI - Model class based on Class::DBI
 
-sub get_objects {
-    my ($self, $r) = @_;
-    return $self->retrieve(shift @{$r->{args}});
-}
+=head1 DESCRIPTION
+
+This is a master model class which uses C<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.
+
+=cut
 
 sub related {
     my ($self, $r) = @_;
@@ -62,12 +67,13 @@ sub adopt {
 }
 
 sub search :Exported {
+    return shift->SUPER::search(@_) if caller eq "Class::DBI"; # oops
     my ($self, $r) = @_;
     my %fields = map {$_ => 1 } $self->columns;
     my $oper = "like"; # For now
+    use Carp; Carp::confess("Urgh") unless ref $r;
     my %params = %{$r->{params}};
-    my %values = map { $_ => {$oper, $oper eq "like" ? "%".$params{$_}."%" 
-                                                     :$params{$_} } }
+    my %values = map { $_ => {$oper, $params{$_} } }
                  grep { $params{$_} and $fields{$_} } keys %params;
 
     $r->objects([ %values ? $self->search_where(%values) : $self->retrieve_all ]);
@@ -75,5 +81,13 @@ sub search :Exported {
     $r->{template_args}{search} = 1;
 }
 
-
+sub list :Exported {
+    my ($self, $r) = @_;
+    if ( my $rows = $r->config->{rows_per_page}) {
+        $self = $self->pager($rows, $r->query->{page});
+        $r->{template_args}{pager} = $self;
+    } 
+    $r->objects([ $self->retrieve_all ]);
+}
 1;
+