[Koha-devel] [RFC: DBIx::Class 3/3] DBIx::Class - Changes to C4::Members to start using DBIx::Class

Andrew Moore andrew.moore at liblime.com
Fri Oct 24 16:10:44 CEST 2008


Here are some changes that can be made to the C4::Members class to start moving
away from writing our own SQL and towards using the C4::Schema object built with
DBIx::Class. Though I haven't replaced many methods in this class, I'm chosing this
one because it's more complex than the C4::ClassSource example and may show some
more aspects of my proposed change.
---
 C4/Members.pm |   25 +++++++++++--------------
 1 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/C4/Members.pm b/C4/Members.pm
index e756ffb..c9cbd0d 100644
--- a/C4/Members.pm
+++ b/C4/Members.pm
@@ -108,6 +108,9 @@ BEGIN {
 	);
 }
 
+use C4::Schema;
+our $schema = C4::Schema->connect();
+
 =head1 NAME
 
 C4::Members - Perl Module containing convenience functions for member handling
@@ -592,7 +595,7 @@ sub GetMemberIssuesAndFines {
 }
 
 sub columns(;$) {
-    return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from borrowers")};
+    return $schema->source('Borrowers')->columns;
 }
 
 =head2
@@ -633,9 +636,9 @@ sub ModMember {
     }
     my @columns = &columns;
     my %hashborrowerfields = (map {$_=>1} @columns);
-    my $query = "UPDATE borrowers SET \n";
-    my $sth;
-    my @parameters;  
+
+    my $borrower = $schema->resultset('Borrowers')->find( $data{'borrowernumber'} );
+    return unless $borrower; # that member must not exist.
     
     # test to know if you must update or not the borrower password
     if (exists $data{password}) {
@@ -649,21 +652,15 @@ sub ModMember {
     foreach (keys %data) {  
         next if ($_ eq 'borrowernumber' or $_ eq 'flags');
         if ($hashborrowerfields{$_}){
-            $query .= " $_=?, "; 
-            push @parameters,$data{$_};
+            $borrower->set_column( $_, $data{$_} );
         } else {
             push @badkeys, $_;
             delete $data{$_};
         }
     }
     (@badkeys) and warn scalar(@badkeys) . " Illegal key(s) passed to ModMember: " . join(',', at badkeys);
-    $query =~ s/, $//;
-    $query .= " WHERE borrowernumber=?";
-    push @parameters, $data{'borrowernumber'};
-    $debug and print STDERR "$query (executed w/ arg: $data{'borrowernumber'})";
-    $sth = $dbh->prepare($query);
-    my $execute_success = $sth->execute(@parameters);
-    $sth->finish;
+    $debug and print STDERR "updating borrower: $data{'borrowernumber'}";
+    my $execute_success = $borrower->update();
 
 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
 # so when we update information for an adult we should check for guarantees and update the relevant part
@@ -673,7 +670,7 @@ sub ModMember {
         # is adult check guarantees;
         UpdateGuarantees(%data);
     }
-    logaction("MEMBERS", "MODIFY", $data{'borrowernumber'}, "$query (executed w/ arg: $data{'borrowernumber'})") 
+    logaction("MEMBERS", "MODIFY", $data{'borrowernumber'}, "modified borrower: $data{'borrowernumber'}") 
         if C4::Context->preference("BorrowersLog");
 
     return $execute_success;
-- 
1.5.6




More information about the Koha-devel mailing list