From: Moritz Onken Date: Sun, 10 May 2009 15:37:16 +0000 (+0000) Subject: set_$rel accepts now a $link_vals hashref like add_to_$rel does X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ac36a402;p=dbsrgits%2FDBIx-Class-Historic.git set_$rel accepts now a $link_vals hashref like add_to_$rel does --- diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 23df27e..64ae001 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -470,7 +470,7 @@ B relationships.> =over 4 -=item Arguments: (\@hashrefs | \@objs) +=item Arguments: (\@hashrefs | \@objs), $link_vals? =back @@ -481,6 +481,10 @@ B relationships.> $actor->set_roles(\@roles); # Replaces all of $actor's previous roles with the two named + $actor->set_roles(\@roles, { salary => 15_000_000 }); + # Sets a column in the link table for all roles + + Replace all the related objects with the given reference to a list of objects. This does a C B to remove the association between the current object and all related objects, then calls diff --git a/lib/DBIx/Class/Relationship/ManyToMany.pm b/lib/DBIx/Class/Relationship/ManyToMany.pm index 163ac36..a99f7d5 100644 --- a/lib/DBIx/Class/Relationship/ManyToMany.pm +++ b/lib/DBIx/Class/Relationship/ManyToMany.pm @@ -103,7 +103,7 @@ EOW ); my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_); $self->search_related($rel, {})->delete; - $self->$add_meth($_) for (@to_set); + $self->$add_meth($_, ref($_[1]) ? $_[1] : {}) for (@to_set); }; my $remove_meth_name = join '::', $class, $remove_meth; diff --git a/t/66relationship.t b/t/66relationship.t index 80c5524..a7f8771 100644 --- a/t/66relationship.t +++ b/t/66relationship.t @@ -8,7 +8,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 74; +plan tests => 78; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -189,6 +189,14 @@ is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj) count ok' ); is( $prod_rs->first->name, 'Matt S Trout', 'many_to_many add_to_$rel($obj) ok' ); $cd->remove_from_producers($prod); +$cd->add_to_producers($prod, {attribute => 1}); +is( $prod_rs->count(), 1, 'many_to_many add_to_$rel($obj, $link_vals) count ok' ); +is( $cd->cd_to_producer->first->attribute, 1, 'many_to_many $link_vals ok'); +$cd->remove_from_producers($prod); +$cd->set_producers([$prod], {attribute => 2}); +is( $prod_rs->count(), 1, 'many_to_many set_$rel($obj, $link_vals) count ok' ); +is( $cd->cd_to_producer->first->attribute, 2, 'many_to_many $link_vals ok'); +$cd->remove_from_producers($prod); is( $schema->resultset('Producer')->find(1)->name, 'Matt S Trout', "producer object exists after remove of link" ); is( $prod_rs->count, 0, 'many_to_many remove_from_$rel($obj) ok' ); @@ -234,6 +242,7 @@ is( $twokey->fourkeys->count, 0, 'twokey has no fourkeys' ); is( $twokey->fourkeys_to_twokeys->count, 0, 'twokey has no links to fourkey' ); + my $undef_artist_cd = $schema->resultset("CD")->new_result({ 'title' => 'badgers', 'year' => 2007 }); is($undef_artist_cd->has_column_loaded('artist'), '', 'FK not loaded'); is($undef_artist_cd->search_related('artist')->count, 0, '0=1 search when FK does not exist and object not yet in db'); diff --git a/t/lib/DBICTest/Schema/CD_to_Producer.pm b/t/lib/DBICTest/Schema/CD_to_Producer.pm index cf18c4e..d8a9cc3 100644 --- a/t/lib/DBICTest/Schema/CD_to_Producer.pm +++ b/t/lib/DBICTest/Schema/CD_to_Producer.pm @@ -7,6 +7,7 @@ __PACKAGE__->table('cd_to_producer'); __PACKAGE__->add_columns( cd => { data_type => 'integer' }, producer => { data_type => 'integer' }, + attribute => { data_type => 'integer', is_nullable => 1 }, ); __PACKAGE__->set_primary_key(qw/cd producer/); diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql index 9e8aaa6..6b66ab4 100644 --- a/t/lib/sqlite.sql +++ b/t/lib/sqlite.sql @@ -108,6 +108,7 @@ CREATE UNIQUE INDEX cd_artist_title_cd ON cd (artist, title); CREATE TABLE cd_to_producer ( cd integer NOT NULL, producer integer NOT NULL, + attribute integer, PRIMARY KEY (cd, producer) );