]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Maypole/Model/CDBI.pm
Miscellaneous fixes.
[maypole.git] / lib / Maypole / Model / CDBI.pm
index 83c118ae271e8eecd910d8f78c758a3d3da33a53..0be76f98d40e453ba961333006e23aa06e038bf4 100644 (file)
@@ -3,6 +3,7 @@ use base qw(Maypole::Model::Base Class::DBI);
 use Lingua::EN::Inflect::Number qw(to_PL);
 use Class::DBI::AsForm;
 use Class::DBI::FromCGI;
+use Class::DBI::Loader;
 use Class::DBI::AbstractSearch;
 use Class::DBI::Plugin::RetrieveAll;
 use Class::DBI::Pager;
@@ -34,7 +35,7 @@ sub related {
 sub do_edit :Exported {
     my ($self, $r) = @_;
     my $h = CGI::Untaint->new(%{$r->{params}});
-    my ($obj) = @{$r->objects};
+    my ($obj) = @{$r->objects || []};
     if ($obj) {
         # We have something to edit
         $obj->update_from_cgi($h);
@@ -55,8 +56,9 @@ sub do_edit :Exported {
 }
 
 sub delete :Exported {
+    return shift->SUPER::delete(@_) if caller ne "Maypole::Model::Base";
     my ($self, $r) = @_;
-    $_->SUPER::delete for @{ $r->objects };
+    $_->SUPER::delete for @{ $r->objects || [] };
     $r->objects([ $self->retrieve_all ]);
     $r->{template} = "list";
 }
@@ -64,11 +66,14 @@ sub delete :Exported {
 sub adopt {
     my ($self, $child) = @_;
     $child->autoupdate(1);
-    $child->columns( Stringify => qw/ name / );
+    if (grep { $_ eq "name" } $child->columns) { # Common case
+        $child->columns( Stringify => qw/ name / );
+    } # Otherwise, work it out for yourself.
 }
 
 sub search :Exported {
-    return shift->SUPER::search(@_) if caller eq "Class::DBI"; # oops
+    return shift->SUPER::search(@_) if caller ne "Maypole::Model::Base";
+                                    # A real CDBI search.
     my ($self, $r) = @_;
     my %fields = map {$_ => 1 } $self->columns;
     my $oper = "like"; # For now
@@ -98,5 +103,24 @@ sub list :Exported {
         $r->objects([ $self->retrieve_all ]);
     }
 }
+
+sub setup_database {
+    my ($self, $config, $namespace, $dsn, $u, $p) = @_;
+    $config->{dsn} = $dsn;
+    $config->{loader} = Class::DBI::Loader->new(
+        namespace => $namespace,
+        dsn => $dsn,
+        user => $u,
+        password => $p,
+    );
+    $config->{classes} = [ $config->{loader}->classes ];
+    $config->{tables}  = [ $config->{loader}->tables ];
+}
+
+sub class_of {
+    my ($self, $r, $table) = @_;
+    return $r->config->{loader}->_table2class($table);
+}
+
 1;