patch from rjbs to add by_key contructor to Record.pm
authorivan <ivan>
Tue, 12 Jul 2005 09:13:19 +0000 (09:13 +0000)
committerivan <ivan>
Tue, 12 Jul 2005 09:13:19 +0000 (09:13 +0000)
CREDITS
FS/FS/Record.pm

diff --git a/CREDITS b/CREDITS
index e2ebcd2..0e60599 100644 (file)
--- a/CREDITS
+++ b/CREDITS
@@ -150,5 +150,8 @@ by Foteos Macrides (derived from overLIB <http://www.bosrup.com/web/overlib/>
 by Erik Bosrup), licensed under the terms of the Artistic license
 <http://www.macridesweb.com/oltest/license.html>.
 
+Ricardo SIGNES <rjbs+freeside-devel@icgroup.com> has contributed a bunch of
+patches to clean up and refactor various stuff in the module layer.  Thanks!
+
 Everything else is my (Ivan Kohler <ivan@420.am>) fault.
 
index 5c8a322..f806e4f 100644 (file)
@@ -427,6 +427,34 @@ sub qsearch {
   return @return;
 }
 
+=item by_key PRIMARY_KEY_VALUE
+
+This is a class method that returns the record with the given primary key
+value.  This method is only useful in FS::Record subclasses.  For example:
+
+  my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
+
+is equivalent to:
+
+  my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
+
+=cut
+
+sub by_key {
+  my ($class, $pkey_value) = @_;
+
+  my $table = $class->table
+    or croak "No table for $class found";
+
+  my $dbdef_table = $dbdef->table($table)
+    or die "No schema for table $table found - ".
+           "do you need to create it or run dbdef-create?";
+  my $pkey = $dbdef_table->primary_key
+    or die "No primary key for table $table";
+
+  return qsearchs($table, { $pkey => $pkey_value });
+}
+
 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
 
 Experimental JOINed search method.  Using this method, you can execute a