]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Maypole/Model/CDBI.pm
fix tabs/spaces
[maypole.git] / lib / Maypole / Model / CDBI.pm
index 171c9164b21de239d2dcde9e75f20200eebb2765..4a83bcb08f5c402c91bb22bd04497a5208dde688 100644 (file)
@@ -22,6 +22,57 @@ 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.
 
+It implements a base set of methods required for a Maypole Data Model.
+See L<Maypole::Model::Base> for these:
+
+=over 4
+
+=item adopt
+
+=item class_of
+
+=item do_edit
+
+=item list
+
+=item related
+
+=item setup_database
+
+=item fetch_objects
+
+=back 
+
+=head1 Additional Commands
+
+=over 
+
+=item delete
+
+Surprisingly, this command causes a database record to be forever lost.
+
+=item search
+
+The search action 
+
+=back
+
+=head1 Helper Methods
+
+=over 
+
+=item order
+
+=item stringify_column
+
+=item do_pager
+
+=item related_class
+
+Given an accessor name as a method, this function returns the class this accessor returns.
+
+=back
+
 =cut
 
 sub related {
@@ -29,6 +80,18 @@ sub related {
     return keys %{ $self->meta_info('has_many') || {} };
 }
 
+sub related_class {
+    my ( $self, $r, $accessor ) = @_;
+    my $related = $self->related->{$accessor};
+    if ( my $mapping = $related->{args}->{mapping} ) {
+        return $related->{foreign_class}->meta_info('has_a')->{ $$mapping[0] }
+          ->{foreign_class};
+    }
+    else {
+        return $related->{foreign_class};
+    }
+}
+
 sub do_edit : Exported {
     my ( $self, $r ) = @_;
     my $h        = CGI::Untaint->new( %{ $r->{params} } );
@@ -97,7 +160,7 @@ sub search : Exported {
     my $oper   = "like";                                # For now
     my %params = %{ $r->{params} };
     my %values = map { $_ => { $oper, $params{$_} } }
-      grep { $params{$_} and $fields{$_} } keys %params;
+      grep { length ($params{$_}) and $fields{$_} } keys %params;
 
     $r->template("list");
     if ( !%values ) { return $self->list($r) }
@@ -106,7 +169,7 @@ sub search : Exported {
     $r->objects(
         [
             $self->search_where(
-                \%values, ( $order ? { order => $order } : () )
+                \%values, ( $order ? { order_by => $order } : () )
             )
         ]
     );
@@ -145,12 +208,13 @@ 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,
@@ -162,6 +226,8 @@ sub setup_database {
     );
     $config->{classes} = [ $config->{loader}->classes ];
     $config->{tables}  = [ $config->{loader}->tables ];
+    warn( 'Loaded tables: ' . join ',', @{ $config->{tables} } )
+      if $namespace->debug;
 }
 
 sub class_of {
@@ -169,5 +235,15 @@ 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;