an underlying search_rs(), as by design these arguments would be
used only on the first call to ->related_resultset(), and ignored
afterwards. Instead an exception (detailing the fix) is thrown.
+ - Calling the set_* many-to-many helper with a list (instead of an
+ arrayref) now emits a deprecation warning
* New Features
- DBIx::Class::Optional::Dependencies now properly understands
my $set_meth_name = join '::', $class, $set_meth;
*$set_meth_name = subname $set_meth_name, sub {
+
my $self = shift;
- @_ > 0 or $self->throw_exception(
- "{$set_meth} needs a list of objects or hashrefs"
+
+ my $set_to = ( ref $_[0] eq 'ARRAY' )
+ ? ( shift @_ )
+ : do {
+ carp_unique(
+ "Calling '$set_meth' with a list of items to link to is deprecated, use an arrayref instead"
+ );
+
+ # gobble up everything from @_ into a new arrayref
+ [ splice @_ ]
+ }
+ ;
+
+ # make sure folks are not invoking a bizarre mix of deprecated and curent syntax
+ $self->throw_exception(
+ "'$set_meth' expects an arrayref of objects or hashrefs to link to, and an optional hashref of link data"
+ ) if (
+ @_ > 1
+ or
+ ( @_ and ref $_[0] ne 'HASH' )
);
my $guard = $self->result_source->schema->storage->txn_scope_guard;
$self->search_related( $rel, {} )->delete;
}
# add in the set rel objects
- $self->$add_meth($_, ref($_[1]) ? $_[1] : {})
- for ( ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_ );
+ $self->$add_meth(
+ $_,
+ @_, # at this point @_ is either empty or contains a lone link-data hash
+ ) for @$set_to;
$guard->commit;
};
use Test::More;
use Test::Exception;
+use Test::Warn;
use lib qw(t/lib);
use DBICTest ':DiffSQL';
'many_to_many add_to_$rel($hash) ok' );
$cd->add_to_producers({ name => 'Jack Black' });
is( $prod_rs->count(), 2, 'many_to_many add_to_$rel($hash) count ok' );
-$cd->set_producers($schema->resultset('Producer')->all);
-is( $cd->producers->count(), $prod_before_count+2,
- 'many_to_many set_$rel(@objs) count ok' );
-$cd->set_producers($schema->resultset('Producer')->find(1));
-is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' );
+
+warnings_like {
+ $cd->set_producers($schema->resultset('Producer')->all);
+ is( $cd->producers->count(), $prod_before_count+2,
+ 'many_to_many set_$rel(@objs) count ok' );
+
+ $cd->set_producers($schema->resultset('Producer')->find(1));
+ is( $cd->producers->count(), 1, 'many_to_many set_$rel($obj) count ok' );
+} [
+ ( qr/\QCalling 'set_producers' with a list of items to link to is deprecated, use an arrayref instead/ ) x 2
+], 'Warnings on deprecated invocation of set_* found';
+
$cd->set_producers([$schema->resultset('Producer')->all]);
is( $cd->producers->count(), $prod_before_count+2,
'many_to_many set_$rel(\@objs) count ok' );
);
# can't use the opaque one - need set_from_related to work
- $artwork->set_artist_limited_rank( @artists );
+ $artwork->set_artist_limited_rank( \@artists );
{
local $TODO = 'Taking into account the relationship bridge condition is not likely to ever work... unless we get DQ hooked somehow';