From: Matt S Trout Date: Sat, 21 Oct 2006 03:16:47 +0000 (+0000) Subject: I hate you all. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=096f421241e5abd4274e4344b873b8570ab17d43;p=dbsrgits%2FDBIx-Class-Historic.git I hate you all. --- diff --git a/lib/DBIx/Class/Core.pm b/lib/DBIx/Class/Core.pm index 4f9a59c..7c48181 100644 --- a/lib/DBIx/Class/Core.pm +++ b/lib/DBIx/Class/Core.pm @@ -8,8 +8,8 @@ use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/ Serialize::Storable - InflateColumn Relationship + InflateColumn PK::Auto PK Row diff --git a/lib/DBIx/Class/InflateColumn.pm b/lib/DBIx/Class/InflateColumn.pm index de68b23..6b06cb0 100644 --- a/lib/DBIx/Class/InflateColumn.pm +++ b/lib/DBIx/Class/InflateColumn.pm @@ -214,7 +214,7 @@ inflation and deflation of columns appropriately. 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}); } @@ -234,7 +234,8 @@ sub new { 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; diff --git a/lib/DBIx/Class/Relationship/Accessor.pm b/lib/DBIx/Class/Relationship/Accessor.pm index b20eb16..b77ce00 100644 --- a/lib/DBIx/Class/Relationship/Accessor.pm +++ b/lib/DBIx/Class/Relationship/Accessor.pm @@ -62,4 +62,39 @@ sub add_relationship_accessor { } } +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; diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 6ab3ec0..64bcd4e 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -343,6 +343,22 @@ sub find { $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, @@ -1234,10 +1250,10 @@ sub new_result { 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; } diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 60250b6..57f883f 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -41,6 +41,9 @@ sub new { 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); diff --git a/t/93single_accessor_object.t b/t/93single_accessor_object.t index 6423c03..8c2db94 100644 --- a/t/93single_accessor_object.t +++ b/t/93single_accessor_object.t @@ -23,20 +23,20 @@ plan tests => 7; 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'); } diff --git a/t/lib/DBICTest/Schema/LinerNotes.pm b/t/lib/DBICTest/Schema/LinerNotes.pm index 013cf91..168f311 100644 --- a/t/lib/DBICTest/Schema/LinerNotes.pm +++ b/t/lib/DBICTest/Schema/LinerNotes.pm @@ -14,5 +14,8 @@ DBICTest::Schema::LinerNotes->add_columns( }, ); DBICTest::Schema::LinerNotes->set_primary_key('liner_id'); +DBICTest::Schema::LinerNotes->belongs_to( + 'cd', 'DBICTest::Schema::CD', 'liner_id' +); 1;