[Dbix-class] Looking for the DBIC way.

Daniel Westermann-Clark dwc at pobox.com
Tue Nov 7 05:13:30 GMT 2006


On 2006-11-06 09:56:28 -0500, Mike Earley wrote:
>     SELECT me.artistid, me.name,
>            cds_left_outer.cdid, cds_left_outer.artist, cds_left_outer.title
>     FROM   artist me
>     LEFT OUTER JOIN cd cds_left_outer ON ( cds_left_outer.artist = 
>     me.artistid )
>     ORDER BY cds_left_outer.artist
> 
> In the DBI version I did not need to program nested loops to access
> the child data.  I'm wondering if I'm not seeing some basic DBIC
> accessor trick? Is there a different, more DBIC, way to code this or
> does the has_many nature of the relationship force me to use nested
> loops to access the data?

Assuming you've got a belongs_to relationship defined on
MyDatabase::Main::Cd back to the artist, you should be able to do
something like:

my $cd_rs = $schema->resultset('Cd')->search(
  undef,
  { prefetch => 'artist' }
);

while (my $cd = $cd_rs->next) {
  print join('|', $cd->artist->artistid, $cd->artist->name, $cd->cdid,
    $cd->artist, $cd->title), "\n";
}

Some tweaking of the join_type might be necessary, but I think it's
feasible.

-- 
Daniel Westermann-Clark



More information about the Dbix-class mailing list