[Catalyst-commits] r8088 - in CatalystX-CRUD/CatalystX-CRUD/trunk: . lib/CatalystX/CRUD

karpet at dev.catalyst.perl.org karpet at dev.catalyst.perl.org
Tue Jul 8 15:49:15 BST 2008


Author: karpet
Date: 2008-07-08 15:49:15 +0100 (Tue, 08 Jul 2008)
New Revision: 8088

Modified:
   CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
   CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
Log:
allow for run-time determination of primary key

Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/Changes	2008-07-08 12:07:07 UTC (rev 8087)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/Changes	2008-07-08 14:49:15 UTC (rev 8088)
@@ -129,4 +129,5 @@
 
 0.28    xxx
         * API for ModelAdapter changed to pass controller instance in do_model()
+        * add get_primary_key() method to base Controller
 

Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm	2008-07-08 12:07:07 UTC (rev 8087)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm	2008-07-08 14:49:15 UTC (rev 8088)
@@ -139,13 +139,42 @@
     my ( $self, $c, $id ) = @_;
     $c->stash->{object_id} = $id;
     $c->log->debug("fetching id = $id") if $c->debug;
-    my @arg = $id ? ( $self->primary_key() => $id ) : ();
+    my $pk = $self->get_primary_key( $c, $id );
+    $c->log->debug("fetching $pk = $id") if $c->debug;
+    my @arg = $id ? ( $pk => $id ) : ();
     $c->stash->{object} = $self->do_model( $c, 'fetch', @arg );
     if ( $self->has_errors($c) or !$c->stash->{object} ) {
         $self->throw_error( 'No such ' . $self->model_name );
     }
 }
 
+=head2 get_primary_key( I<context>, I<pk_value> )
+
+Should return the name of the field to fetch() I<pk_value> from.
+The default behaviour is to return $self->primary_key.
+However, if you have other unique fields in your schema, you
+might return a unique field other than the primary key.
+This allows for a more flexible URI scheme.
+
+A good example is Users. A User record might have a numerical id (uid)
+and a username, both of which are unique. So if username 'foobar'
+has a primary key (uid) of '1234', both these URIs could fetch the same
+record:
+
+ /uri/for/user/1234
+ /uri/for/user/foobar
+
+Again, the default behaviour is to return the primary_key field name
+from config() (accessed via $self->primary_key) but you can override
+get_primary_key() in your subclass to provide more flexibility.
+
+=cut
+
+sub get_primary_key {
+    my ( $self, $c, $id ) = @_;
+    return $self->primary_key;
+}
+
 =head2 create
 
 Attribute: Local




More information about the Catalyst-commits mailing list