Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add linkback to ResultSource::resultset_attributes

Also clarify problems related to its use
  • Loading branch information...
commit 6bf7e59400fd71ec3d3e27155b354ee2369e49c4 1 parent dad42de
@SineSwiper SineSwiper authored ribasushi committed
Showing with 35 additions and 4 deletions.
  1. +4 −0 lib/DBIx/Class/ResultSet.pm
  2. +31 −4 lib/DBIx/Class/ResultSource.pm
View
4 lib/DBIx/Class/ResultSet.pm
@@ -3750,6 +3750,10 @@ searching for data. They can be passed to any method which takes an
C<\%attrs> argument. See L</search>, L</search_rs>, L</find>,
L</count>.
+Default attributes can be set on the result class using
+L<DBIx::Class::ResultSource/resultset_attributes>. (Please read
+the CAVEATS on that feature before using it!)
+
These are in no particular order:
=head2 order_by
View
35 lib/DBIx/Class/ResultSource.pm
@@ -1034,9 +1034,9 @@ exists.
=over 4
-=item Arguments: \%attrs
+=item Arguments: L<\%attrs|DBIx::Class::ResultSet/ATTRIBUTES>
-=item Return Value: \%attrs
+=item Return Value: L<\%attrs|DBIx::Class::ResultSet/ATTRIBUTES>
=back
@@ -1047,8 +1047,35 @@ exists.
$source->resultset_attributes({ order_by => [ 'id' ] });
Store a collection of resultset attributes, that will be set on every
-L<DBIx::Class::ResultSet> produced from this result source. For a full
-list see L<DBIx::Class::ResultSet/ATTRIBUTES>.
+L<DBIx::Class::ResultSet> produced from this result source.
+
+B<CAVEAT>: C<resultset_attributes> comes with its own set of issues and
+bugs! While C<resultset_attributes> isn't deprecated per se, its usage is
+not recommended!
+
+Since relationships use attributes to link tables together, the "default"
+attributes you set may cause unpredictable and undesired behavior. Furthermore,
+the defaults cannot be turned off, so you are stuck with them.
+
+In most cases, what you should actually be using are project-specific methods:
+
+ package My::Schema::ResultSet::Artist;
+ use base 'DBIx::Class::ResultSet';
+ ...
+
+ # BAD IDEA!
+ #__PACKAGE__->resultset_attributes({ prefetch => 'tracks' });
+
+ # GOOD IDEA!
+ sub with_tracks { shift->search({}, { prefetch => 'tracks' }) }
+
+ # in your code
+ $schema->resultset('Artist')->with_tracks->...
+
+This gives you the flexibility of not using it when you don't need it.
+
+For more complex situations, another solution would be to use a virtual view
+via L<DBIx::Class::ResultSource::View>.
=cut
Please sign in to comment.
Something went wrong with that request. Please try again.