From: Norbert Buchmuller Date: Mon, 12 Jan 2009 17:18:22 +0000 (+0100) Subject: * Implemented $rs->current_source_alias. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bbdff861fda1a940df76b265dd66b49f7b440a12;p=dbsrgits%2FDBIx-Class-Historic.git * Implemented $rs->current_source_alias. --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index ca0712d..7eb88f9 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2094,6 +2094,50 @@ sub related_resultset { }; } +=head2 current_source_alias + +=over 4 + +=item Arguments: none + +=item Return Value: $source_alias + +=back + +Returns the current alias of the result source that corrensponds to the result +set (this alias will eventually be used as the SQL table alias in the SQL +query). Usually it is C. + +Currently the source alias that refers to the result set returned by a +L/L family method depends on how you got to the resultset: it's +C by default, but eg. L aliases it to the related result +source name (and keeps C referring to the original result set). The long +term goal is to make L always alias the current resultset as C +(and make this method unnecessary). + +Thus it's currently necessary to use this method in predefined queries (see +L) when referring to the +source alias of the current result set: + + # in a result set class + sub modified_by { + my ($self, $user) = @_; + + my $me = $self->current_source_alias; + + return $self->search( + "$me.modified" => $user->id, + ); + } + +=cut + +sub current_source_alias { + my ($self) = @_; + + return ($self->{attrs} || {})->{alias} || 'me'; +} + sub _resolve_from { my ($self, $extra_join) = @_; my $source = $self->result_source; diff --git a/t/76select.t b/t/76select.t index 2d60873..4325a70 100644 --- a/t/76select.t +++ b/t/76select.t @@ -8,7 +8,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 11; +plan tests => 12; my $rs = $schema->resultset('CD')->search({}, { @@ -61,3 +61,5 @@ my $subsel = $cds->search ({}, { is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds'); is ($subsel->next->title, $cds->next->title, 'First CD title match'); is ($subsel->next->title, $cds->next->title, 'Second CD title match'); + +is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');