}
}
+ ## merge with the conditions from $self (inherited conditions)
+ my ($inherited_cond) = $self->_merge_with_cond({});
+ delete @{$inherited_cond}{@names};
+ my @inherited_names = keys %$inherited_cond;
+ my @values;
+ foreach my $row (@$data) {
+ my %row_data;
+ @row_data{@names} = @{$row}{@names};
+ my ($merged_cond) = $self->_merge_with_cond(\%row_data);
+ push @values, [ @{$merged_cond}{@names, @inherited_names} ];
+ }
+ push @names, @inherited_names;
+
## do bulk insert on current row
$self->result_source->storage->insert_bulk(
$self->result_source,
$self->throw_exception( "new_result needs a hash" )
unless (ref $values eq 'HASH');
- my %new;
+ my ($merged_cond, $from_resultset) = $self->_merge_with_cond($values);
+
+ my %new = (
+ %$merged_cond,
+ @$from_resultset
+ ? (-from_resultset => $from_resultset)
+ : (),
+ -source_handle => $self->_source_handle,
+ -result_source => $self->result_source, # DO NOT REMOVE THIS, REQUIRED
+ );
+
+ return $self->result_class->new(\%new);
+}
+
+# _merge_with_cond
+#
+# Merges $values (a hashref) with the condition in the resultset and returns
+# the resulting hashref and an arrayref that contains the keys that are coming
+# from related resultsets.
+
+sub _merge_with_cond {
+ my ($self, $values) = @_;
+
+ my (%merged_cond, @from_resultset);
+
my $alias = $self->{attrs}{alias};
if (
defined $self->{cond}
&& $self->{cond} eq $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION
) {
- %new = %{ $self->{attrs}{related_objects} || {} }; # nothing might have been inserted yet
- $new{-from_resultset} = [ keys %new ] if keys %new;
+ %merged_cond = %{ $self->{attrs}{related_objects} || {} }; # nothing might have been inserted yet
+ @from_resultset = keys %merged_cond;
} else {
$self->throw_exception(
"Can't abstract implicit construct, condition not a hash"
# precendence must be given to passed values over values inherited from
# the cond, so the order here is important.
- my %implied = %{$self->_remove_alias($collapsed_cond, $alias)};
- while( my($col,$value) = each %implied ){
- if(ref($value) eq 'HASH' && keys(%$value) && (keys %$value)[0] eq '='){
- $new{$col} = $value->{'='};
+ my %implied = %{$self->_remove_alias($collapsed_cond, $alias)};
+ while ( my($col, $value) = each %implied ) {
+ if (ref($value) eq 'HASH' && keys(%$value) && (keys %$value)[0] eq '=') {
+ $merged_cond{$col} = $value->{'='};
next;
}
- $new{$col} = $value if $self->_is_deterministic_value($value);
+ $merged_cond{$col} = $value if $self->_is_deterministic_value($value);
}
}
- %new = (
- %new,
+ %merged_cond = (
+ %merged_cond,
%{ $self->_remove_alias($values, $alias) },
- -source_handle => $self->_source_handle,
- -result_source => $self->result_source, # DO NOT REMOVE THIS, REQUIRED
);
- return $self->result_class->new(\%new);
+ return (\%merged_cond, \@from_resultset);
}
# _is_deterministic_value
use lib qw(t/lib);
use DBICTest;
-plan tests => 142;
-
## ----------------------------------------------------------------------------
## Get a Schema and some ResultSets we can play with.
my $art_rs = $schema->resultset('Artist');
my $cd_rs = $schema->resultset('CD');
+my $restricted_art_rs = $art_rs->search({rank => 42});
+
ok( $schema, 'Got a Schema object');
ok( $art_rs, 'Got Good Artist Resultset');
ok( $cd_rs, 'Got Good CD Resultset');
is($cdB->artist->name, 'Fred BloggsD', 'Set Artist to FredD');
ok($cdB->artist->artistid == $aid, "Got Expected Artist ID");
}
+
+ WITH_COND_FROM_RS: {
+
+ my ($more_crap) = $restricted_art_rs->populate([
+ {
+ name => 'More Manufactured Crap',
+ },
+ ]);
+
+ ## Did it use the condition in the resultset?
+ cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object");
+ }
}
ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
}
+ WITH_COND_FROM_RS: {
+
+ $restricted_art_rs->populate([
+ {
+ name => 'VOID More Manufactured Crap',
+ },
+ ]);
+
+ my $more_crap = $art_rs->search({
+ name => 'VOID More Manufactured Crap'
+ })->first;
+
+ ## Did it use the condition in the resultset?
+ cmp_ok( $more_crap->rank, '==', 42, "Got Correct rank for result object");
+ }
}
ARRAYREF_OF_ARRAYREF_STYLE: {
is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name';
is $cool->name, 'An actually cool singer.', 'Correct Name';
+ #cmp_ok $cool->rank, '==', 42, 'Correct Rank';
+
my ($cooler, $lamer) = $art_rs->populate([
[qw/artistid name/],
[1003, 'Cooler'],
is $cooler->name, 'Cooler', 'Correct Name';
is $lamer->name, 'Lamer', 'Correct Name';
-}
\ No newline at end of file
+
+ #cmp_ok $cooler->rank, '==', 42, 'Correct Rank';
+
+ ARRAY_CONTEXT_WITH_COND_FROM_RS: {
+
+ my ($mega_lamer) = $restricted_art_rs->populate([
+ {
+ name => 'Mega Lamer',
+ },
+ ]);
+
+ ## Did it use the condition in the resultset?
+ cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object");
+ }
+
+ VOID_CONTEXT_WITH_COND_FROM_RS: {
+
+ $restricted_art_rs->populate([
+ {
+ name => 'VOID Mega Lamer',
+ },
+ ]);
+
+ my $mega_lamer = $art_rs->search({
+ name => 'VOID Mega Lamer'
+ })->first;
+
+ ## Did it use the condition in the resultset?
+ cmp_ok( $mega_lamer->rank, '==', 42, "Got Correct rank for result object");
+ }
+}
+
+done_testing;