__PACKAGE__->load_components(qw/
Serialize::Storable
- InflateColumn
Relationship
+ InflateColumn
PK::Auto
PK
Row
sub update {
my ($class, $attrs, @rest) = @_;
foreach my $key (keys %{$attrs||{}}) {
- if (ref $attrs->{$key}
+ if (ref $attrs->{$key} && $class->has_column($key)
&& exists $class->column_info($key)->{_inflate_info}) {
$class->set_inflated_column($key, delete $attrs->{$key});
}
my $inflated;
foreach my $key (keys %{$attrs||{}}) {
$inflated->{$key} = delete $attrs->{$key}
- if ref $attrs->{$key} && exists $class->column_info($key)->{_inflate_info};
+ if ref $attrs->{$key} && $class->has_column($key)
+ && exists $class->column_info($key)->{_inflate_info};
}
my $obj = $class->next::method($attrs, @rest);
$obj->{_inflated_column} = $inflated if $inflated;
}
}
+sub new {
+ my ($class, $attrs, @rest) = @_;
+ my ($related, $info);
+ foreach my $key (keys %{$attrs||{}}) {
+ next unless $info = $class->relationship_info($key);
+ $related->{$key} = delete $attrs->{$key}
+ if ref $attrs->{$key}
+ && $info->{attrs}{accessor}
+ && $info->{attrs}{accessor} eq 'single';
+ }
+ my $obj = $class->next::method($attrs, @rest);
+ if ($related) {
+ $obj->{_relationship_data} = $related;
+ foreach my $rel (keys %$related) {
+ $obj->set_from_related($rel, $related->{$rel});
+ }
+ }
+ return $obj;
+}
+
+sub update {
+ my ($obj, $attrs, @rest) = @_;
+ my $info;
+ foreach my $key (keys %{$attrs||{}}) {
+ next unless $info = $obj->relationship_info($key);
+ if (ref $attrs->{$key} && $info->{attrs}{accessor}
+ && $info->{attrs}{accessor} eq 'single') {
+ my $rel = delete $attrs->{$key};
+ $obj->set_from_related($key => $rel);
+ $obj->{_relationship_data}{$key} = $rel;
+ }
+ }
+ return $obj->next::method($attrs, @rest);
+}
+
1;
$input_query = {@_};
}
+ my (%related, $info);
+
+ foreach my $key (keys %$input_query) {
+ if (ref($input_query->{$key})
+ && ($info = $self->result_source->relationship_info($key))) {
+ my $rel_q = $self->result_source->resolve_condition(
+ $info->{cond}, delete $input_query->{$key}, $key
+ );
+ die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY';
+ @related{keys %$rel_q} = values %$rel_q;
+ }
+ }
+ if (my @keys = keys %related) {
+ @{$input_query}{@keys} = values %related;
+ }
+
my @unique_queries = $self->_unique_queries($input_query, $attrs);
# Build the final query: Default to the disjunction of the unique queries,
my %new = (
%{ $self->_remove_alias($values, $alias) },
%{ $self->_remove_alias($collapsed_cond, $alias) },
+ -result_source => $self->result_source,
);
my $obj = $self->result_class->new(\%new);
- $obj->result_source($self->result_source) if $obj->can('result_source');
return $obj;
}
if ($attrs) {
$new->throw_exception("attrs must be a hashref")
unless ref($attrs) eq 'HASH';
+ if (my $source = delete $attrs->{-result_source}) {
+ $new->result_source($source);
+ }
foreach my $k (keys %$attrs) {
$new->throw_exception("No such column $k on $class")
unless $class->has_column($k);
is($cd->get_column('artist'), $artist->id, 'artist matches CD');
my $liner_notes = $schema->resultset("LinerNotes")->find_or_create({
- liner_id => $cd,
- notes => "Creating using an object on a might_have is helpful.",
+ cd => $cd,
+ notes => "Creating using an object on a might_have is helpful.",
});
ok(defined $liner_notes, 'created liner notes');
is($liner_notes->liner_id, $cd->cdid, 'liner notes matches CD');
is($liner_notes->notes, "Creating using an object on a might_have is helpful.", 'liner notes are correct');
my $track = $cd->tracks->find_or_create({
- position => 1,
+ position => 127,
title => 'Single Accessor'
});
is($track->get_column('cd'), $cd->cdid, 'track matches CD before update');
my $another_cd = $schema->resultset("CD")->find(5);
$track->update({ disc => $another_cd });
- is($track->cdid, $another_cd->cdid, 'track matches another CD after update');
+ is($track->get_column('cd'), $another_cd->cdid, 'track matches another CD after update');
}
},
);
DBICTest::Schema::LinerNotes->set_primary_key('liner_id');
+DBICTest::Schema::LinerNotes->belongs_to(
+ 'cd', 'DBICTest::Schema::CD', 'liner_id'
+);
1;