[Catalyst-commits] r8162 - in CatalystX-CRUD/CatalystX-CRUD/trunk:
. lib/CatalystX/CRUD lib/CatalystX/CRUD/ModelAdapter
lib/CatalystX/CRUD/Test
karpet at dev.catalyst.perl.org
karpet at dev.catalyst.perl.org
Fri Jul 25 16:36:37 BST 2008
Author: karpet
Date: 2008-07-25 16:36:36 +0100 (Fri, 25 Jul 2008)
New Revision: 8162
Modified:
CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Form.pm
Log:
prelim support for multiple-column PKs and fix a couple failing tests
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/Changes 2008-07-25 06:56:31 UTC (rev 8161)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/Changes 2008-07-25 15:36:36 UTC (rev 8162)
@@ -129,5 +129,5 @@
0.28 xxx
* API for ModelAdapter changed to pass controller instance in do_model()
- * add get_primary_key() method to base Controller
+ * add get_primary_key() method to base Controller. This allows for PKs composed of multiple columns.
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm 2008-07-25 06:56:31 UTC (rev 8161)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm 2008-07-25 15:36:36 UTC (rev 8162)
@@ -138,10 +138,9 @@
sub fetch : Chained('/') PathPrefix CaptureArgs(1) {
my ( $self, $c, $id ) = @_;
$c->stash->{object_id} = $id;
- $c->log->debug("fetching id = $id") if $c->debug;
- my $pk = $self->get_primary_key( $c, $id );
- $c->log->debug("fetching $pk = $id") if $c->debug;
- my @arg = $id ? ( $pk => $id ) : ();
+ $c->log->debug("fetch $id") if $c->debug;
+ my @pk = $self->get_primary_key( $c, $id );
+ my @arg = $id ? (@pk) : ();
$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 );
@@ -150,29 +149,46 @@
=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.
+Should return an array of the name of the field(s) to fetch() I<pk_value> from
+and their respective values.
+
+The default behaviour is to return B<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
+has a B<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
+Again, the default behaviour is to return the B<primary_key> field name(s)
from config() (accessed via $self->primary_key) but you can override
get_primary_key() in your subclass to provide more flexibility.
+If your primary key is composed of multiple columns, your return value
+should include all those columns and their values as extracted
+from I<pk_value>. Multiple values are assumed to be joined with C<;;>.
+
=cut
sub get_primary_key {
my ( $self, $c, $id ) = @_;
- return $self->primary_key;
+ my $pk = $self->primary_key;
+ my @ret;
+ if ( ref $pk ) {
+ my @val = split( m/;;/, $id );
+ for my $col (@$pk) {
+ push( @ret, $col => shift @val );
+ }
+ }
+ else {
+ @ret = ( $pk => $id );
+ }
+ return @ret;
}
=head2 create
@@ -708,6 +724,9 @@
=item primary_key
+primary_key may be a single column name or an array ref of multiple
+column names.
+
=item page_size
=item allow_GET_writes
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm 2008-07-25 06:56:31 UTC (rev 8161)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm 2008-07-25 15:36:36 UTC (rev 8162)
@@ -39,7 +39,7 @@
=cut
sub new_object {
- my ( $self, $c, @arg ) = @_;
+ my ( $self, $controller, $c, @arg ) = @_;
my $model = $c->model( $self->model_name );
$model->new_object(@arg);
}
@@ -51,7 +51,7 @@
=cut
sub fetch {
- my ( $self, $c, @arg ) = @_;
+ my ( $self, $controller, $c, @arg ) = @_;
my $model = $c->model( $self->model_name );
$model->fetch(@arg);
}
@@ -63,7 +63,7 @@
=cut
sub search {
- my ( $self, $c, @arg ) = @_;
+ my ( $self, $controller, $c, @arg ) = @_;
my $model = $c->model( $self->model_name );
$model->search(@arg);
}
@@ -75,7 +75,7 @@
=cut
sub iterator {
- my ( $self, $c, @arg ) = @_;
+ my ( $self, $controller, $c, @arg ) = @_;
my $model = $c->model( $self->model_name );
$model->iterator(@arg);
}
@@ -87,7 +87,7 @@
=cut
sub count {
- my ( $self, $c, @arg ) = @_;
+ my ( $self, $controller, $c, @arg ) = @_;
my $model = $c->model( $self->model_name );
$model->count(@arg);
}
@@ -99,7 +99,7 @@
=cut
sub make_query {
- my ( $self, $c, @arg ) = @_;
+ my ( $self, $controller, $c, @arg ) = @_;
my $model = $c->model( $self->model_name );
$model->make_query(@arg);
}
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Form.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Form.pm 2008-07-25 06:56:31 UTC (rev 8161)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Form.pm 2008-07-25 15:36:36 UTC (rev 8162)
@@ -66,6 +66,10 @@
This must be set in new().
+=head2 field_names
+
+An alias for fields().
+
=head2 params( [ I<hashref> ] )
Get/set the hashref of key/value pairs for the form object. The keys should
More information about the Catalyst-commits
mailing list