From: Will Hawes Date: Fri, 27 Jan 2006 09:08:17 +0000 (+0000) Subject: add cookbook example for multi-step prefetch X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=897342e412d5803d73f34ef23b3bdead1835325e;p=dbsrgits%2FDBIx-Class-Historic.git add cookbook example for multi-step prefetch --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index ec8948e..c22e947 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -231,8 +231,7 @@ main query. Five CDs, five extra queries. A hundred CDs, one hundred extra queries! Thankfully, L has a C attribute to solve this problem. -This allows you to fetch results from a related table as well as the main table -for your class: +This allows you to fetch results from related tables in advance: my $rs = $schema->resultset('CD')->search( { @@ -325,6 +324,31 @@ notes: # WHERE liner_notes.notes LIKE '%some text%' # AND author.name = 'A. Writer' +=head2 Multi-step prefetch + +From 0.04999_05 onwards, C can be nested more than one relationship +deep using the same syntax as a multi-step join: + + my $rs = $schema->resultset('Tag')->search( + {}, + { + prefetch => { + cd => 'artist' + } + } + ); + + # Equivalent SQL: + # SELECT tag.*, cd.*, artist.* FROM tag + # JOIN cd ON tag.cd = cd.cdid + # JOIN artist ON cd.artist = artist.artistid + +Now accessing our C and C relationships does not need additional +SQL statements: + + my $tag = $rs->first; + print $tag->cd->artist->name; + =head2 Transactions As of version 0.04001, there is improved transaction support in