X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBase.pm;h=e2efc9987253bc4e9831ecb4a10de13f8c29994f;hb=ef0845bad4b2945f8d5bb4157ba3aa9fe95ef790;hp=42d09553d84b17dc8455084c9fe368fb71a415ea;hpb=98def3efbed614ff1514c79b9da7e03b5ceb06c0;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 42d0955..e2efc99 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -181,11 +181,14 @@ L and the resulting SQL will be used verbatim as the C clause of the C statement associated with this relationship. While every coderef-based condition must return a valid C clause, it may -elect to additionally return a simplified join-free condition hashref when -invoked as C<< $result->relationship >>, as opposed to +elect to additionally return a simplified B join-free condition +hashref when invoked as C<< $result->$relationship >>, as opposed to C<< $rs->related_resultset('relationship') >>. In this case C<$result> is -passed to the coderef as C<< $args->{self_result_object} >>, so a user can do the -following: +passed to the coderef as C<< $args->{self_result_object} >>. Alternatively +the user-space could be calling C<< $result->set_from_related( $rel => +$foreign_related_object ) >>, in which case C<$foreign_related_object> will +be passed to the coderef as C<< $args->{foreign_result_object >>. In other +words if you define your condition coderef as: sub { my $args = shift; @@ -195,14 +198,17 @@ following: "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" }, "$args->{foreign_alias}.year" => { '>', "1979", '<', "1990" }, }, - $args->{self_result_object} && { + ! $args->{self_result_object} ? () : { "$args->{foreign_alias}.artist" => $args->{self_result_object}->artistid, "$args->{foreign_alias}.year" => { '>', "1979", '<', "1990" }, }, + ! $args->{foreign_result_object} ? () : { + "$args->{self_alias}.artistid" => $args->{foreign_result_object}->artist, + } ); } -Now this code: +Then this code: my $artist = $schema->resultset("Artist")->find({ id => 4 }); $artist->cds_80s->all; @@ -219,6 +225,14 @@ With the bind values: '4', '1990', '1979' +While this code: + + my $cd = $schema->resultset("CD")->search({ artist => 1 }, { rows => 1 })->single; + my $artist = $schema->resultset("Artist")->new({}); + $artist->set_from_related('cds_80s'); + +Will properly set the C<< $artist->artistid >> field of this new object to C<1> + Note that in order to be able to use L<< $result->create_related|DBIx::Class::Relationship::Base/create_related >>, the coderef must not only return as its second such a "simple" condition