]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/commitdiff
m2m accepts one element instead of an arrayref; id as generic pk
authorzby <zby@bd8105ee-0ff8-0310-8827-fb3f25b6796d>
Tue, 12 May 2009 19:21:58 +0000 (19:21 +0000)
committerzby <zby@bd8105ee-0ff8-0310-8827-fb3f25b6796d>
Tue, 12 May 2009 19:21:58 +0000 (19:21 +0000)
lib/DBIx/Class/ResultSet/RecursiveUpdate.pm
t/lib/DBSchema/Result/Dvd.pm
t/lib/DBSchema/Result/Dvdtag.pm
t/lib/DBSchema/Result/Viewing.pm
t/lib/RunTests.pm
t/var/dvdzbr.db

index 0997ff3a8bf854ea4412fcfe89487a9ead358fb6..f288135edd9f2bf01838c142aa3f85d02990ed5f 100644 (file)
@@ -45,6 +45,20 @@ sub recursive_update {
     my %columns_by_accessor = _get_columns_by_accessor( $self );
     for my $name ( keys %$updates ) {
         my $source = $self->result_source;
+        if( $name eq 'id'
+#            && scalar @{$source->primary_columns} == 1
+            && !$source->has_column( 'id' )
+        ){
+            my @ids = ( $updates->{id} );
+            if( ref $updates->{id} ){
+                @ids = @{ $updates->{id} };
+            }
+            my $i = 0;
+            for my $key ( $source->primary_columns ){
+                $columns{ $key } = $ids[ $i++ ];
+            }
+            next;
+        }
         if ( $columns_by_accessor{$name}
             && !( $source->has_relationship($name) && ref( $updates->{$name} ) )
           )
@@ -77,10 +91,9 @@ sub recursive_update {
         $object = $self->find( \%columns, { key => 'primary' } );
     }
     $object ||= $self->new( {} );
-
     # first update columns and other accessors - so that later related records can be found
     for my $name ( keys %columns ) {
-        $object->$name( $updates->{$name} );
+        $object->$name( $columns{$name} );
     }
     for my $name ( keys %other_methods) {
         $object->$name( $updates->{$name} ) if $object->can( $name );
@@ -103,7 +116,14 @@ sub recursive_update {
             my ($pk) = _get_pk_for_related( $self, $name);
             my @rows;
             my $result_source = $object->$name->result_source;
-            for my $elem ( @{ $updates->{$name} } ) {
+            my @updates;
+            if( ref $updates->{$name} ){
+                @updates = @{ $updates->{$name} };
+            }
+            else{
+                @updates = ( $updates->{$name} );
+            }
+            for my $elem ( @updates ) {
                 if ( ref $elem ) {
                     push @rows, $result_source->resultset->find($elem);
                 }
index e791b76fc721c16e15760fd321fac3ff5dbfbd80..0020109318b104d4a2b222c51bb3e78f02ce77f5 100644 (file)
@@ -8,11 +8,10 @@ use warnings;
 use base 'DBIx::Class';
 use overload '""' => sub {$_[0]->name}, fallback => 1;
 
-use lib '../../DBIx-Class-HTML-FormFu/lib/';
 __PACKAGE__->load_components(qw/IntrospectableM2M Core/);
 __PACKAGE__->table('dvd');
 __PACKAGE__->add_columns(
-  'id' => {
+  'dvd_id' => {
     data_type => 'integer',
     is_auto_increment => 1
   },
@@ -45,11 +44,11 @@ __PACKAGE__->add_columns(
     is_nullable => 1,
   },
 );
-__PACKAGE__->set_primary_key('id');
+__PACKAGE__->set_primary_key('dvd_id');
 __PACKAGE__->belongs_to('owner', 'DBSchema::Result::User', { id => 'owner' });
 __PACKAGE__->belongs_to('current_borrower', 'DBSchema::Result::User', { id => 'current_borrower' });
-__PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.id' });
-__PACKAGE__->has_many('viewings', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.id' });
+__PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.dvd_id' });
+__PACKAGE__->has_many('viewings', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.dvd_id' });
 __PACKAGE__->many_to_many('tags', 'dvdtags' => 'tag');
 __PACKAGE__->might_have(
     liner_notes => 'DBSchema::Result::LinerNotes', undef,
index d17066aff0e82b4b5b4005a5d15075d58be3d652..68bdde9b931ca295fa633bb934b8c4c6d8885aae 100644 (file)
@@ -14,7 +14,7 @@ __PACKAGE__->add_columns(
     "tag" => { data_type => 'integer' },
 );
 __PACKAGE__->set_primary_key("dvd", "tag");
-__PACKAGE__->belongs_to("dvd", "DBSchema::Result::Dvd", { id => "dvd" });
+__PACKAGE__->belongs_to("dvd", "DBSchema::Result::Dvd", { dvd_id => "dvd" });
 __PACKAGE__->belongs_to("tag", "DBSchema::Result::Tag", { id => "tag" });
 
 1;
index 13dcaa0626ebe4f9cb11a4badf699a923ca0ea52..bf7e7ef6907d934091ad9cb37b5f54adaa11fb22 100755 (executable)
@@ -15,7 +15,7 @@ __PACKAGE__->belongs_to(
 
 __PACKAGE__->belongs_to( 
     dvd => 'DBSchema::Result::Dvd',
-    {'foreign.id'=>'self.dvd_id'},
+    {'foreign.dvd_id'=>'self.dvd_id'},
 );
 
 ;
index fd4fe71d9cf655003c477a1c0767fc6696952247..b5f447d20d4b172b8c085cf1cb2288f7d7fb6c2e 100644 (file)
@@ -9,7 +9,7 @@ use Test::More;
 sub run_tests{
     my $schema = shift;
 
-    plan tests => 29;
+    plan tests => 30;
     
     my $dvd_rs = $schema->resultset( 'Dvd' );
     my $user_rs = $schema->resultset( 'User' );
@@ -19,7 +19,7 @@ sub run_tests{
     my $initial_user_count = $user_rs->count;
     my $initial_dvd_count = $dvd_rs->count;
     my $updates;
-   
+
     # creating new record linked to some old record
     $updates = {
             name => 'Test name 2',
@@ -81,7 +81,7 @@ sub run_tests{
 
     my $num_of_users = $user_rs->count;
     $updates = {
-            id => $dvd->id,
+            id => $dvd->dvd_id, # id instead of dvd_id
             aaaa => undef,
             name => undef,
             tags => [ ], 
@@ -96,14 +96,15 @@ sub run_tests{
             },
 
     };
-    $dvd = $dvd_rs->recursive_update( $updates );
-    
+    my $dvd_updated = $dvd_rs->recursive_update( $updates );
+   
+    is ( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' );
     is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
-    is ( $dvd->name, undef, 'Dvd name deleted' );
-    is ( $dvd->owner->id, $another_owner->id, 'Owner updated' );
-    is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' );
-    is ( $dvd->tags->count, 0, 'Tags deleted' );
-    is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' );
+    is ( $dvd_updated->name, undef, 'Dvd name deleted' );
+    is ( $dvd_updated->owner->id, $another_owner->id, 'Owner updated' );
+    is ( $dvd_updated->current_borrower->name, 'new name a', 'Related record modified' );
+    is ( $dvd_updated->tags->count, 0, 'Tags deleted' );
+    is ( $dvd_updated->liner_notes->notes, 'test note changed', 'might_have record changed' );
 
     # repeatable
     
index c31073daa62f17ac817e3cb592b941aa0a032fa6..f96d3d59c461516e086df8304cd25e8db8fdeea5 100644 (file)
Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ
This page took 0.027521 seconds and 4 git commands to generate.